package com.nantian.cloud.dbcapp.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.nantian.cloud.dbcapp.entity.AccessEnterpriseInfo;
import com.nantian.cloud.dbcapp.service.AccessEnterpriseInfoService;
import com.nantian.cloud.dbcapp.service.AccessUnionService;
import com.nantian.cloud.dbcapp.tool.StatisticalDetails;
import com.nantian.cloud.dbcapp.vo.AccessUnionQuotaVo;
import com.nantian.cloud.dbcapp.vo.AccessUnionVo;
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.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;

@CrossOrigin
@RestController
@AllArgsConstructor
@RequestMapping("/accessEnterpriseInfo")
@Api(value = "accessEnterpriseInfo", tags = "延期接入企业-基本信息")
public class AccessEnterpriseInfoController {

    @Autowired
    private AccessEnterpriseInfoService infoService;

    @Autowired
    private AccessUnionService accessUnionService;

    @GetMapping("/page")
    @SysLog("分页获取企业基本信息")
    @ApiOperation(value = "分页获取企业基本信息", notes = "分页获取企业基本信息")
    public R getAdvancedMaterialPage(Page page, AccessEnterpriseInfo info) {
        /*return R.ok(infoService.page(page, new QueryWrapper<AccessEnterpriseInfo>()
                .eq("del_flag","1")
                .like("state",info.getState()!=null ? info.getState() : "")
                .like("region_code",info.getRegionCode()!=null ? info.getRegionCode() : "")
                .like("industry_code",info.getIndustryCode()!=null ? info.getIndustryCode() : "")
                .like("cmp_name",info.getCmpName()!=null ? info.getCmpName() : "")
                .like("enterprise_code",info.getEnterpriseCode()!=null ? info.getEnterpriseCode() : "")
        ));*/
        Map<String,Object> map = new HashMap<>();
        IPage<AccessEnterpriseInfo> pageData = infoService.getPage(page,info);
        map.put("pageData",pageData);
        List<Map<String,Object>> groupData = infoService.groupData(info);
        map.put("groupData",groupData);
        return R.ok(map);
    }

    @GetMapping("/infoByEpc")
    @SysLog("获取企业基本信息")
    @ApiOperation(value = "获取企业基本信息", notes = "获取企业基本信息")
    public R infoByEpc(String epc) {
       return  R.ok(infoService.getOne(new QueryWrapper<AccessEnterpriseInfo>()
               .eq("del_flag","1")
               .eq("enterprise_code",epc!=null ? epc : "")));
    }

    @PostMapping("/upInfo")
    @SysLog("修改企业基本信息")
    @ApiOperation(value = "修改企业基本信息", notes = "修改企业基本信息")
    public R upInfo(@RequestBody AccessEnterpriseInfo info) {
        if(info.getEnterpriseCode()==null){
            return R.failed("企业编码不能为空！");
        }
        AccessEnterpriseInfo old = infoService.getOne(new QueryWrapper<AccessEnterpriseInfo>()
                .eq("del_flag","1")
                .eq("enterprise_code",info.getEnterpriseCode()!=null ? info.getEnterpriseCode() : ""));
        ScUser u = SecurityUtils.getUser();
        info.setUserid(u.getId());
        info.setUpdateTime(new Date());
        info.setDelFlag("1");
        info.setDeptId(u.getDeptId());
        boolean b = false;
        if(old != null){
            info.setId(old.getId());
            b = infoService.updateById(info);
        }else{
            b = infoService.save(info);
        }
        return  R.ok(b);
    }

    @PostMapping("/upState")
    @SysLog("根据id批量修改企业状态")
    @ApiOperation(value = "根据id批量修改企业状态", notes = "根据id批量修改企业状态")
    public R upState(@RequestBody String str) {
        ScUser u = SecurityUtils.getUser();
        JSONObject json = JSONObject.parseObject(str);
        Object state = (Object) JSONObject.parseObject(json.getString("state"), Object.class);
        List<Integer> idList = (List<Integer>) JSONObject.parseObject(json.getString("idList"), Object.class);
        for (Integer o : idList) {
            AccessEnterpriseInfo info = new AccessEnterpriseInfo();
            info.setId(o.longValue());
            info.setState(state.toString());
            info.setUserid(u.getId());
            info.setUpdateTime(new Date());
            info.setDelFlag("1");
            info.setDeptId(u.getDeptId());
            infoService.updateById(info);
        }
        return R.ok();
    }

    @DeleteMapping("/delete")
    @SysLog("根据id删除企业信息")
    @ApiOperation(value = "根据id删除企业信息", notes = "根据id删除企业信息")
    public R removeById(Integer id) {
        AccessEnterpriseInfo info = infoService.getById(id);
        if (info == null) {
            return R.failed("企业信息不存在，删除失败！");
        }
        ScUser u = SecurityUtils.getUser();
        info.setUserid(u.getId());
        info.setUpdateTime(new Date());
        info.setDelFlag("4");
        return R.ok(infoService.updateById(info));
    }


    @ApiOperation(value = "得到延期企业的填报情况",notes = "得到延期企业的填报情况")
    @SysLog("得到延期企业的填报情况")
    @GetMapping(value = "/getunion")
    public R getAccessUnion(String regionCode,String year){
        if(year==null || year == ""){
            return R.failed("查询失败，年份不能为空!");
        }
        String dictid;

        if(regionCode  == null  || regionCode == ""){
            dictid = null;
        }else if(regionCode.length()<4 || !regionCode.startsWith("53") || regionCode.length()>6){
            return R.failed("非法的州市编码！");
        }else {
            dictid=regionCode.substring(0,4);
        }
        List<AccessUnionQuotaVo> unionQuota = accessUnionService.getUnionQuota(dictid, year);
        List<AccessUnionVo> unionTructure = accessUnionService.getUnionTructure(dictid, year);
        List<AccessUnionVo> allInfo = accessUnionService.getAllInfo(null);
        Integer[] infodetial = StatisticalDetails.getInfodetial(allInfo);
        List<Map<String, Object>> tructureDetails = StatisticalDetails.statisticalData(unionTructure,infodetial);
        List<Map<String, Object>> quotaDetails = StatisticalDetails.statisticalDataQuota(unionQuota,infodetial);
        List<Map<String, Object>> tructure;
        List<Map<String, Object>> quota;
        System.out.println(dictid);
        if(dictid == null || dictid == ""){
            tructure = StatisticalDetails.getTructure(unionTructure, allInfo);
            quota = StatisticalDetails.getQuota(unionQuota, allInfo);
        }else {
            tructure=StatisticalDetails.getTructure(unionTructure,allInfo,dictid);
            quota = StatisticalDetails.getQuota(unionQuota, allInfo,dictid);
        }


        Map<String,Object> map = new LinkedHashMap<>();
        map.put("quota",tructure);
        map.put("tructure",quota);
        map.put("quotadetails",quotaDetails);
        map.put("tructuredetails", tructureDetails);
        map.put("allShouldWrite",allInfo);
        return R.ok(map,"查询成功！");
    }
}
