package com.nantian.cloud.jly.controller;

import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.nantian.cloud.jly.entity.CheckAudit;
import com.nantian.cloud.jly.entity.CheckItems;
import com.nantian.cloud.jly.service.CheckItemsService;
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.math.BigInteger;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;


/**
 * 企业审查明细
 *
 * @author
 * @date 2018-09-27 15:20:44
 */
@RestController
@AllArgsConstructor
@Api(value = "checkItems", tags = "企业审查明细")
@RequestMapping("/checkItems")
public class CheckItemsController {
    private final CheckItemsService checkItemsService;


    /**
     * 分页查询
     *
     * @param page       分页对象
     * @param checkItems 审查项对象
     * @return
     */
    @SysLog("企业审查明细-查询")
    @ApiOperation(value = "查询企业审查明细", notes = "条件查询企业审查明细")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "CheckItems", value = "企业审查明细", required = false, dataType = "json", paramType = "path"),
            @ApiImplicitParam(name = "Page", value = "分页对象", required = false, dataType = "json", paramType = "path")
    })
    @GetMapping("/page")
    public R getcheckItemsPage(Page page, CheckItems checkItems) {
        checkItems.setDelFlag("1"); //查询未删除数据
        QueryWrapper<CheckItems> queryWrapper = new QueryWrapper<CheckItems>();
        queryWrapper.lambda().eq(CheckItems::getCheckPlanId, checkItems.getCheckPlanId());
        queryWrapper.lambda().eq(CheckItems::getEpc, checkItems.getEpc());
        queryWrapper.lambda().eq(CheckItems::getDelFlag, checkItems.getDelFlag());
        return R.ok(checkItemsService.page(page, queryWrapper));
    }

    /**
     * 查询审查情况汇总表
     *
     * @return
     */
    @SysLog("企业审查明细-查询")
    @ApiOperation(value = "查询企业审查明细", notes = "条件查询企业审查明细")
    @GetMapping("/query")
    public R getcheckItems(BigInteger checkPlanId, String epc) {
        return R.ok(checkItemsService.queryItems(checkPlanId, epc));
    }

    /**
     * 保存审查情况汇总表
     *
     * @return
     */
    @SysLog("企业审查明细-查询")
    @ApiOperation(value = "保存审查情况汇总表", notes = "保存审查情况汇总表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "checkItems", value = "企业审查明细", required = false, dataType = "json", paramType = "path"),
    })
    @PostMapping("/save")
    public R saveCheckItems(@RequestBody List<CheckItems> checkItems) {
        List<CheckItems> newList = new ArrayList<>();
        for (CheckItems item : checkItems) {
            CheckItems byId = checkItemsService.getById(item.getId());
            byId.setComment(item.getComment());
            byId.setSerial(item.getSerial());
            newList.add(byId);
        }
        return R.ok(checkItemsService.updateBatchById(newList));
    }


    /**
     * 信息
     *
     * @param checkItemsId
     * @return R
     */
    @SysLog("企业审查明细-查询")
    @ApiOperation(value = "通过id查询企业审查明细", notes = "通过id查询企业审查明细")
    @ApiImplicitParam(name = "checkItemsId", value = "企业审查明细", required = true, paramType = "path", dataType = "int")
    @PostMapping("/{checkItemsId}")
    public R getById(@PathVariable("checkItemsId") Integer checkItemsId) {
        return R.ok(checkItemsService.getById(checkItemsId));
    }

    /**
     * 保存
     *
     * @param checkItems
     * @return R
     */
    @SysLog("企业审查明细-保存")
    @ApiOperation(value = "保存企业审查明细", notes = "保存企业审查明细")
    @ApiImplicitParam(name = "CheckItems", value = "企业审查明细", required = true, dataType = "json", paramType = "path")
    @PostMapping("/saveBatch")
    public R saveBatch(@RequestBody List<CheckItems> checkItems) {
        ScUser u = SecurityUtils.getUser();
        checkItems.forEach(r -> {
            if (u.getId() != null) {
                r.setUserid(u.getId());
            }
            if (u.getDeptId() != null) {
                r.setDeptId(u.getDeptId());
            }
            r.setDelFlag("1");
            r.setUpdateTime(new Date());
        });
        return R.ok(checkItemsService.saveOrUpdateBatch(checkItems));
    }

    /**
     * 保存
     *
     * @param checkItems
     * @return R
     */
    @SysLog("企业审查明细-保存")
    @ApiOperation(value = "保存企业审查明细", notes = "保存企业审查明细")
    @ApiImplicitParam(name = "CheckItems", value = "企业审查明细", required = true, dataType = "json", paramType = "path")
    @PostMapping
    public R save(@RequestBody CheckItems checkItems) {
        ScUser u = SecurityUtils.getUser();
        if (u.getId() != null) {
            checkItems.setUserid(u.getId());
        }
        if (u.getDeptId() != null) {
            checkItems.setDeptId(u.getDeptId());
        }
        checkItems.setDelFlag("1");
        checkItems.setUpdateTime(new Date());
        return R.ok(checkItemsService.save(checkItems));
    }

    /**
     * 修改
     *
     * @param checkItems
     * @return R
     */
    @SysLog("企业审查明细-修改")
    @ApiOperation(value = "修改企业审查明细", notes = "修改企业审查明细")
    @ApiImplicitParam(name = "CheckItems", value = "企业审查明细", required = true, dataType = "json", paramType = "path")
    @PutMapping
    public R updateById(@RequestBody CheckItems checkItems) {
        ScUser u = SecurityUtils.getUser();
        if (u.getId() != null) {
            checkItems.setUserid(u.getId());
        }
        checkItems.setUpdateTime(new Date());
        return R.ok(checkItemsService.updateById(checkItems));
    }

    /**
     * 删除
     *
     * @param checkItemsId
     * @return R
     */
    @SysLog("企业审查明细-删除")
    @ApiOperation(value = "删除企业审查明细", notes = "删除企业审查明细")
    @ApiImplicitParam(name = "checkItemsId", value = "企业审查明细", required = false, dataType = "int", paramType = "path")
    @DeleteMapping("/{checkItemsId}")
    public R removeById(@PathVariable("checkItemsId") Long checkItemsId) {
        CheckItems checkItems = checkItemsService.getById(checkItemsId);
        ScUser u = SecurityUtils.getUser();
        if (u.getId() != null) {
            checkItems.setUserid(u.getId());
        }
        checkItems.setDelFlag("4");
        checkItems.setUpdateTime(new Date());
        return R.ok(checkItemsService.updateById(checkItems));
    }


    /**
     * 根据计划ID和企业行用代码查询审查项结果
     *
     * @param checkPlanId 审查计划ID
     * @param epc         企业信用代码
     * @param checkResult 审查结果
     * @return R
     */
    @SysLog("企业审查明细-根据计划ID和企业行用代码查询审查项结果")
    @ApiOperation(value = "查询审查结论的不符合项数据", notes = "查询审查结论的不符合项数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "checkPlanId", value = "审查计划id", required = true, dataType = "string", paramType = "path"),
            @ApiImplicitParam(name = "epc", value = "企业信用代码", required = true, dataType = "string", paramType = "path"),
            @ApiImplicitParam(name = "checkResult", value = "审查结果", required = false, dataType = "string", paramType = "path")
    })
    @GetMapping("/getCheckItemsForReport")
    public R getCheckItemsForReport(String checkPlanId, String epc, String checkResult) {
        return R.ok(checkItemsService.getCheckItemsForReport(checkPlanId, epc, checkResult));
    }

    /**
     * 不符合项修改保存
     *
     * @param checkItems
     * @return R
     */
    @SysLog("企业审查明细-不符合项修改")
    @ApiOperation(value = "不符合项修改", notes = "不符合项修改")
    @ApiImplicitParam(name = "CheckItems", value = "不符合项", required = true, dataType = "json", paramType = "path")
    @PutMapping("/update")
    public synchronized R updateByCheckItemsNon(@RequestBody CheckItems checkItems) {
//        ScUser u= SecurityUtils.getUser();
//        if(u.getId()!=null){
//            checkItems.setUserid(u.getId());
//        }
//        checkItems.setUpdateTime(new Date());
//        return R.ok(checkItemsService.updateBatchById(checkItems));
        CheckItems byId = checkItemsService.getById(checkItems.getId());
        byId.setNonCode(checkItems.getNonCode());
        byId.setNonCase(checkItems.getNonCase());
        byId.setNonReq(checkItems.getNonReq());
        return R.ok(checkItemsService.updateById(byId));
    }


    @SysLog("企业审查明细-查询不符合项")
    @GetMapping("/getNoN")
    public R getNoN(Integer checkPlanId, String epcList) {
        String[] s = epcList.split(",");
        return R.ok(checkItemsService.getNoN(checkPlanId, s));
//        return R.ok("");
    }

}
