package com.nantian.cloud.jljc.controller;

import cn.hutool.crypto.SecureUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.nantian.cloud.jljc.entity.AdvancedMaterial;
import com.nantian.cloud.jljc.entity.EnterpriseInfo;
import com.nantian.cloud.jljc.entity.StandardLibrary;
import com.nantian.cloud.jljc.entity.TradeKind;
import com.nantian.cloud.jljc.service.EnterpriseInfoService;
import com.nantian.cloud.jljc.service.StandardLibraryService;
import com.nantian.cloud.jljc.service.TradeKindService;
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.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

@CrossOrigin
@RestController
@AllArgsConstructor
@RequestMapping("/StandardLibrary")
@Api(value = "StandardLibrary", tags = "标准库")
public class StandardLibraryController {
    private StandardLibraryService standardLibraryService;
    private TradeKindService tradeKindService;
    private EnterpriseInfoService enterpriseInfoService;

    @GetMapping("/selectall")
    @SysLog("根据条件查询标准库（企业端）")
    @ApiOperation(value = "根据条件查询标准库（企业端）", notes = "根据条件查询标准库（企业端）")
    @ApiImplicitParam(name = "id", value = "id", required = true, dataType = "json", paramType = "path")
    public R selectall(Page page, StandardLibrary standardLibrary){
        ScUser u = SecurityUtils.getUser();
        standardLibrary.setEpc(u.getEnterpriseCode());
        standardLibrary.setDelFlag("1");  //筛选未删除项
        return R.ok( standardLibraryService.selectall(page,standardLibrary));
    }

    @PutMapping("/update")
    @SysLog("修改标准库")
    @ApiOperation(value = "修改标准库", notes = "修改标准库")
    @ApiImplicitParam(name = "standardLibrary", value = "先进技术资料对象", required = true, dataType = "Boolean", paramType = "path")
    public boolean updateAll(@RequestBody StandardLibrary standardLibrary) throws ParseException {
        ScUser u = SecurityUtils.getUser();
        standardLibrary.setUserid(u.getId());
        standardLibrary.setUpdateTime(new Date());
        /*TradeKind tradeKind= new TradeKind();
        tradeKind.setCode(standardLibrary.getIndustryCode());
        tradeKind = tradeKindService.selectbycode(tradeKind);
        standardLibrary.setIndustry(tradeKind.getName());*/
        return standardLibraryService.update(getTradeName(standardLibrary));

    }

    @DeleteMapping("/deleteall")
    @SysLog("删除标准信息")
    @ApiOperation(value = "删除标准信息", notes = "删除标准信息")
    @ApiImplicitParam(name = "standardLibrary", value = "standardLibrary", required = true, dataType = "boolean", paramType = "path")
    public Boolean delete(@RequestBody StandardLibrary standardLibrary) {

        ScUser u = SecurityUtils.getUser();
        standardLibrary.setUserid(u.getId());
        standardLibrary.setDelFlag("4");
        standardLibrary.setUpdateTime(new Date());
        //return standardLibraryService.deleteall(standardLibrary);
        return standardLibraryService.updateById(standardLibrary);
    }


    @PostMapping("/insertall")
    @SysLog("新增标准信息")
    @ApiOperation(value = "新增标准信息", notes = "新增标准信息")
    @ApiImplicitParam(name = "standardLibrary", value = "standardLibrary", required = true, dataType = "boolean", paramType = "path")
    public Boolean insertALL(@RequestBody StandardLibrary standardLibrary) {
        ScUser u = SecurityUtils.getUser();
        standardLibrary.setUserid(u.getId());
        //standardLibrary.setDeptId(u.getDeptId());
        standardLibrary.setDelFlag("1");
        standardLibrary.setUpdateTime(new Date());
        standardLibrary.setEpc(u.getEnterpriseCode());
        //获取企业信息
        EnterpriseInfo info = enterpriseInfoService.getInfoByCode(u.getEnterpriseCode());
        //企业端新增信息，添加deptid
        standardLibrary.setDeptId(info != null ? Integer.parseInt(info.getRegionCode()) : u.getDeptId());
        return standardLibraryService.insertall(getTradeName(standardLibrary));
    }

    private StandardLibrary getTradeName(@RequestBody StandardLibrary standardLibrary) {
        if(standardLibrary.getIndustryCode()!=null){
            TradeKind tradeKind= new TradeKind();
            tradeKind.setCode(standardLibrary.getIndustryCode());
            if(tradeKindService.selectbycode(tradeKind)!=null){
                standardLibrary.setIndustry(tradeKindService.selectbycode(tradeKind).getName());
            }
        }
       if(standardLibrary.getProductCategoryNum()!=null) {
           TradeKind tradeKind2 = new TradeKind();
           tradeKind2.setCode(standardLibrary.getProductCategoryNum());
           if (tradeKindService.selectbycode(tradeKind2) != null) {
               standardLibrary.setProductCategory(tradeKindService.selectbycode(tradeKind2).getName());
           }
       }
        return standardLibrary;
    }
}
