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.CheckBasis;
import com.nantian.cloud.jly.entity.EnterpriseInfo;
import com.nantian.cloud.jly.entity.EnterpriseInfoAmend;
import com.nantian.cloud.jly.service.EnterpriseInfoAmendService;
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.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigInteger;
import java.util.Date;
import java.util.List;


/**
 * 企业基本信息修改表
 *
 * @author
 * @date 2018-09-27 15:20:44
 */
@CrossOrigin
@RestController
@AllArgsConstructor
@RequestMapping("/enterpriseInfoAmend")
@Api(value = "enterpriseInfoAmend", tags = "企业基本信息修改")
public class EnterpriseInfoAmendController {
    private final EnterpriseInfoAmendService enterpriseInfoAmendService;

    @Autowired
    private EnterpriseInfoService enterpriseInfoService;

    /**
     * 分页查询
     *
     * @return
     */
    @SysLog("企业基本信息修改-分页查询")
    @GetMapping("/query")
    @ApiOperation(value = "分页查询企业基本信息", notes = "分页查询企业基本信息")
    public R getEnterpriseInfoPage(BigInteger checkPlanId, String epc) {
        QueryWrapper<EnterpriseInfoAmend> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("check_plan_id", checkPlanId).eq("enterprise_code", epc);
        List<EnterpriseInfoAmend> list = enterpriseInfoAmendService.list(queryWrapper);
        if (list.size()>0){
			return R.ok(list);
		}else {
            QueryWrapper<EnterpriseInfo> queryWrapperPro = new QueryWrapper<>();
            queryWrapperPro.eq("enterprise_code", epc);
            List<EnterpriseInfo> listPro = enterpriseInfoService.list(queryWrapperPro);
            return R.ok(listPro);
		}
    }

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

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

}
