package com.nantian.cloud.jly.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.nantian.cloud.jly.entity.EnterpriseInfo;
import com.nantian.cloud.jly.service.EnterpriseInfoService;
import com.nantian.sc.cloud.common.core.util.R;
import com.nantian.sc.cloud.common.log.annotation.SysLog;
import com.nantian.sc.cloud.common.security.service.ScUser;
import com.nantian.sc.cloud.common.security.util.SecurityUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;


/**
 * 企业基本信息
 *
 * @author
 * @date 2018-09-27 15:20:44
 */
@CrossOrigin
@RestController
@AllArgsConstructor
@RequestMapping("/enterpriseInfo")
@Api(value = "enterpriseInfo", tags = "企业基本信息")
public class EnterpriseInfoController {
    private final EnterpriseInfoService enterpriseInfoService;

    @SysLog("企业基本信息-根据企业信用代码获取企业基本信息")
    @PostMapping("/queryByEnterpriseCode/code")
    @ApiOperation(value = "根据企业信用代码获取企业基本信息", notes = "根据企业信用代码获取企业基本信息")
    @ApiImplicitParam(name = "企业信用代码", value = "enterpriseCode", required = false, paramType = "path")
    public R queryByEnterpriseCode(@RequestBody String s) {
        JSONObject json = JSONObject.parseObject(s);
        String enterpriseCode = json.getString("enterpriseCode");
        EnterpriseInfo enterpriseInfo = new EnterpriseInfo();
        //enterpriseInfo.setId(Long.valueOf(2));
        enterpriseInfo.setEnterpriseCode(enterpriseCode);
        //QueryWrapper<EnterpriseInfo> queryWrapper = new QueryWrapper<>();
        //queryWrapper.lambda().eq(EnterpriseInfo::getEnterpriseCode, enterpriseCode);
        //enterpriseInfoService.getOne(queryWrapper);
        return R.ok(enterpriseInfoService.queryByEpc(enterpriseCode));

        //return R.ok(enterpriseInfoService.getOne(Wrappers.query(enterpriseInfo).select("id, enterprise_code, isorg, org_type, org_code, password, cmp_name, address".split(",")).eq("enterprise_Code",enterpriseCode)));
    }

    @SysLog("企业基本信息-根据企业信用代码获取企业基本信息")
    @GetMapping("/getEnterprise/code")
    @ApiOperation(value = "根据企业信用代码获取企业基本信息", notes = "根据企业信用代码获取企业基本信息")
    @ApiImplicitParam(name = "企业信用代码", value = "enterpriseCode", required = false, paramType = "path")
    public R getByEnterpriseCode(@RequestParam("enterpriseCode") String enterpriseCode) {
        EnterpriseInfo enterpriseInfo = new EnterpriseInfo();
        enterpriseInfo.setEnterpriseCode(enterpriseCode);
        enterpriseInfo.setDelFlag("1");
        return R.ok(enterpriseInfoService.getOne(Wrappers.query(enterpriseInfo)));
    }

    /**
     * 分页查询
     *
     * @param page           分页对象
     * @param enterpriseInfo 请假审批单
     * @return
     */
    @SysLog("企业基本信息-分页查询")
    @GetMapping("/page")
    @ApiOperation(value = "分页查询企业基本信息", notes = "分页查询企业基本信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Page", value = "分页对象", required = false, dataType = "json", paramType = "path"),
            @ApiImplicitParam(name = "EnterpriseInfo", value = "企业基本信息", required = false, dataType = "json", paramType = "path")
    })
    public R getEnterpriseInfoPage(Page page, EnterpriseInfo enterpriseInfo) {
        enterpriseInfo.setDelFlag("1"); //查询未删除数据
        enterpriseInfo.setUrl("");
        return R.ok(enterpriseInfoService.selectPageAll(page, enterpriseInfo));
//		return R.ok(enterpriseInfoService.page(page, Wrappers.query(enterpriseInfo)));

    }
    @SysLog("企业基本信息-在线企业查询")
    @GetMapping("/onlinePage")
    @ApiOperation(value = "在线企业查询", notes = "在线企业查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Page", value = "分页对象", required = false, dataType = "json", paramType = "path"),
            @ApiImplicitParam(name = "EnterpriseInfo", value = "企业基本信息", required = false, dataType = "json", paramType = "path")
    })
    public R getOnlinePage(Page page, EnterpriseInfo enterpriseInfo) {
        return R.ok(enterpriseInfoService.getOnlinePage(page, enterpriseInfo));

    }

    /**
     * 分页查询
     *
     * @param page           分页对象
     * @param enterpriseInfo 企业
     * @return
     */
    @SysLog("企业基本信息-分页查询")
    @GetMapping("/pageAll")
    @ApiOperation(value = "分页查询企业基本信息", notes = "分页查询企业基本信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Page", value = "分页对象", required = false, dataType = "json", paramType = "path"),
            @ApiImplicitParam(name = "EnterpriseInfo", value = "企业基本信息", required = false, dataType = "json", paramType = "path")
    })
    public R getEnterpriseInfoPageAll(Page page, EnterpriseInfo enterpriseInfo) {
        QueryWrapper<EnterpriseInfo> qw = new QueryWrapper<>();
        qw.like("cmp_name", enterpriseInfo.getCmpName() == null ? "" : enterpriseInfo.getCmpName());
        qw.eq("del_flag", "1");
        qw.like("enterprise_code", enterpriseInfo.getEnterpriseCode()== null ? "" :enterpriseInfo.getEnterpriseCode());
        qw.like("state", enterpriseInfo.getState()== null ? "" :enterpriseInfo.getState());
        String code = "";
        if (enterpriseInfo.getRegionCode() != null && enterpriseInfo.getRegionCode() != "") {
            if (enterpriseInfo.getRegionCode().equals("530000")) {
                code = "53";
            } else if (enterpriseInfo.getRegionCode().substring(4).equals("00")) {
                code = enterpriseInfo.getRegionCode().substring(0, 4);
            } else {
                code = enterpriseInfo.getRegionCode();
            }
            qw.like("region_code", code);
        }

//        List<EnterpriseInfo> list = enterpriseInfoService.list(qw);
//		enterpriseInfo.setDelFlag("1"); //查询未删除数据
//		enterpriseInfo.setUrl("");
        return R.ok(enterpriseInfoService.page(page, qw));
//		return R.ok(enterpriseInfoService.page(page, Wrappers.query(enterpriseInfo)));

    }


    /**
     * 查询所有企业列表
     *
     * @param
     * @return
     */
    @SysLog("企业基本信息-查询所有企业列表")
    @GetMapping("/getAllCompany")
    @ApiOperation(value = "查询企业所有列表", notes = "分页查询所有列表")
    public R getAllCompany() {
        return R.ok(enterpriseInfoService.getAllCompany());
    }

    /**
     * 信息
     *
     * @param enterpriseInfoId
     * @return R
     */
    @SysLog("企业基本信息-根据id查询企业基本信息")
    @GetMapping("/{enterpriseInfoId}")
    @ApiOperation(value = "根据id查询企业基本信息", notes = "根据id查询企业基本信息")
    @ApiImplicitParam(name = "enterpriseInfoId", value = "企业基本信息id", required = true, dataType = "int", paramType = "path")
    public R getById(@PathVariable("enterpriseInfoId") Integer enterpriseInfoId) {
        return R.ok(enterpriseInfoService.getById(enterpriseInfoId));
    }

    /**
     * 保存
     *
     * @param enterpriseInfo
     * @return R
     */
    @SysLog("企业基本信息-保存")
    @PostMapping
    @ApiOperation(value = "新增企业基本信息", notes = "新增企业基本信息")
    @ApiImplicitParam(name = "EnterpriseInfo", value = "企业基本信息", required = true, dataType = "json", paramType = "path")
    public R save(@RequestBody EnterpriseInfo enterpriseInfo) {
        ScUser u = SecurityUtils.getUser();
        if (u.getId() != null) {
            enterpriseInfo.setUserid(u.getId());
        }
        if (u.getDeptId() != null) {
            enterpriseInfo.setDeptId(u.getDeptId());
        }
        enterpriseInfo.setDelFlag("1");
        enterpriseInfo.setUpdateTime(new Date());
        return R.ok(enterpriseInfoService.save(enterpriseInfo));
    }

    /**
     * 修改
     *
     * @param enterpriseInfo
     * @return R
     */
    @SysLog("企业基本信息-修改")
    @PutMapping
    @ApiOperation(value = "修改企业基本信息", notes = "修改企业基本信息")
    @ApiImplicitParam(name = "EnterpriseInfo", value = "企业基本信息", required = true, dataType = "json", paramType = "path")
    public R updateById(@RequestBody EnterpriseInfo enterpriseInfo) {
        ScUser u = SecurityUtils.getUser();
        if (u.getId() != null) {
            enterpriseInfo.setUserid(u.getId());
        }
        enterpriseInfo.setUpdateTime(new Date());
        return R.ok(enterpriseInfoService.updateById(enterpriseInfo));
    }

    /**
     * 删除
     *
     * @param enterpriseInfoId
     * @return R
     */
    @SysLog("企业基本信息-删除")
    @DeleteMapping("/{enterpriseInfoId}")
    @ApiOperation(value = "根据id删除企业基本信息", notes = "根据id删除企业基本信息")
    @ApiImplicitParam(name = "enterpriseInfoId", value = "企业基本信息id", required = true, dataType = "int", paramType = "path")
    public R removeById(@PathVariable("enterpriseInfoId") Integer enterpriseInfoId) {
        EnterpriseInfo enterpriseInfo = enterpriseInfoService.getById(enterpriseInfoId);
        ScUser u = SecurityUtils.getUser();
        if (u.getId() != null) {
            enterpriseInfo.setUserid(u.getId());
        }
        enterpriseInfo.setDelFlag("4");
        enterpriseInfo.setUpdateTime(new Date());
        return R.ok(enterpriseInfoService.updateById(enterpriseInfo));
    }

}
