package com.zksy.web.controller.basicData;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zksy.common.annotation.Log;
import com.zksy.common.core.controller.BaseController;
import com.zksy.common.core.domain.AjaxResult;
import com.zksy.common.enums.BusinessType;
import com.zksy.common.utils.file.ExcelUtils;
import com.zksy.system.basicData.domain.CrmHighTechZoneEnterprises;
import com.zksy.system.basicData.service.CrmHighTechZoneEnterprisesService;
import com.zksy.utils.DowntemplateUtil;
import com.zksy.utils.SearchUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 高新区企业（119家）Controller
 *
 * @author sy
 * @date 2024-05-06
 */
@RestController
@RequestMapping("/crmHighTechZoneEnterprises")
@Api(tags = "高新区企业（119家）", description = "高新区企业（119家）desc")
public class CrmHighTechZoneEnterprisesController extends BaseController {

    @Autowired
    private CrmHighTechZoneEnterprisesService service;

    @GetMapping("/getById/{borrowId}")
    @ApiOperation(value = "高新区企业（119家）搜索getById")
    public AjaxResult getById(@PathVariable String borrowId) {
        return AjaxResult.success(service.getById(borrowId));
    }

    @GetMapping("/findByPage")
    @ApiOperation(value = "高新区企业（119家）分页")
    public Page findByPage(long pageNum, long pageSize, String conditionJson) throws Exception {
        return service.page(new Page<>(pageNum, pageSize), SearchUtil.parseWhereSql(conditionJson));
    }

    @GetMapping("/getList")
    @ApiOperation(value = "高新区企业（119家）查询所有")
    public AjaxResult getList(String conditionJson) throws Exception {
        return AjaxResult.success(service.list(SearchUtil.parseWhereSql(conditionJson)));
    }

    @GetMapping("/getCreditScoreList")
    @ApiOperation(value = "高新区企业（119家）查询所有企业总分")
    public AjaxResult getCreditScoreList(@ApiParam(value = "企业信用代码",required = false) @RequestParam(value = "unifiedSocialCreditCode",defaultValue = "") String unifiedSocialCreditCode,
                                         @ApiParam(value = "页码",required = false) @RequestParam(value = "pageNum") int pageNum,
                                         @ApiParam(value = "页数",required = false) @RequestParam(value = "pageSize") int pageSize,
                                         @ApiParam(value = "企业名称",required = false) @RequestParam(value = "enterpriseName",defaultValue = "") String enterpriseName,
                                         @ApiParam(value = "评级参数",required = false) @RequestParam(value = "condition",defaultValue = "") String ... condition) {
        return AjaxResult.success(service.creditScoreList(unifiedSocialCreditCode,enterpriseName,pageNum,pageSize,condition));
    }

    @GetMapping("/getCreditScoreListDetails")
    @ApiOperation(value = "高新区企业（119家）查询所有企业总分详情")
    public AjaxResult getCreditScoreListDetails(@ApiParam(value = "企业信用代码",required = false) @RequestParam(value = "unifiedSocialCreditCode") String unifiedSocialCreditCode,
                                                @ApiParam(value = "页码",required = false) @RequestParam(value = "pageNum") int pageNum,
                                                @ApiParam(value = "页数",required = false) @RequestParam(value = "pageSize") int pageSize,
                                                @ApiParam(value = "企业名称",required = false) @RequestParam(value = "enterpriseName",defaultValue = "") String enterpriseName) {
        return AjaxResult.success(service.creditScoreListDetails(unifiedSocialCreditCode,enterpriseName,pageNum,pageSize));
    }
    @GetMapping("/getCreditScoreMostDetailsByUniCode")
    @ApiOperation(value = "根据统一代码查最详细的分")
    public AjaxResult getCreditScoreListMostDetailsByUniCode(@ApiParam(value = "企业信用代码",required = true) @RequestParam(value = "unifiedSocialCreditCode") String unifiedSocialCreditCode) {
        return service.creditScoreMostDetailsByUniCode(unifiedSocialCreditCode);
    }


    @GetMapping("/enterpriseDetails")
    @ApiOperation(value = "企业明细")
    public AjaxResult enterpriseDetails(@ApiParam(value = "企业信用代码",required = true) @RequestParam(value = "unifiedSocialCreditCode") String unifiedSocialCreditCode) {
        return service.enterpriseDetails(unifiedSocialCreditCode);
    }

    @PostMapping("/download")
    @ApiOperation(value = "高新区企业数据下载")
    public AjaxResult download(@ApiParam(value = "企业信用代码",required = true) @RequestParam(value = "unifiedSocialCreditCode") String unifiedSocialCreditCode,HttpServletResponse response) {
        return AjaxResult.success(service.download(unifiedSocialCreditCode,response));
    }

    @GetMapping("/getDeductionPoints")
    @ApiOperation(value = "获取扣分项总数")
    public AjaxResult getDeductionPoints() {
        return service.getDeductionPoints();
    }

    /**
     * 新增高新区企业（119家）
     */
    @PostMapping("/save")
    @ApiOperation(value = "高新区企业（119家）新增")
    @Log(title = "新增高新区企业（119家）", businessType = BusinessType.INSERT)
    public boolean save(@RequestBody CrmHighTechZoneEnterprises crmHighTechZoneEnterprises) {
        return service.save(crmHighTechZoneEnterprises);
    }

    /**
     * 修改高新区企业（119家）
     */
    @PostMapping("/updateById")
    @ApiOperation(value = "高新区企业（119家）修改")
    @Log(title = "修改高新区企业（119家）", businessType = BusinessType.UPDATE)
    public boolean updateById(@RequestBody CrmHighTechZoneEnterprises crmHighTechZoneEnterprises) {
        return service.updateById(crmHighTechZoneEnterprises);
    }

    /**
     * 导入高新区企业（119家）
     */
    @ApiOperation("高新区企业（119家）导入")
    @Log(title = "高新区企业（119家）", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file)
    {
        try {
            List<CrmHighTechZoneEnterprises> crmHighTechZoneEnterprisesAll = service.list();
            List<CrmHighTechZoneEnterprises> list = ExcelUtils.readMultipartFile(file, CrmHighTechZoneEnterprises.class,CrmHighTechZoneEnterprises.class.getDeclaredFields().length - 2);
            if(crmHighTechZoneEnterprisesAll != null && crmHighTechZoneEnterprisesAll.size() > 0){
                //return AjaxResult.error("数据已存在，如需重新导入请清空数据");
            }
            if(list.size() > 0){
                service.saveBatch(list);
                return success("导入成功，共计："+list.size()+"条");
            }else{
                return AjaxResult.error("无数据");
            }
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.error("导入数据失败");
        }
    }

    @ApiOperation("高新区企业（119家）导出")
    @Log(title = "高新区企业（119家）", businessType = BusinessType.EXPORT)
    @PostMapping("/exportData")
    public AjaxResult exportData(HttpServletResponse response, String conditionJson){
        try {
            List<CrmHighTechZoneEnterprises> wrapperList = service.list(SearchUtil.parseWhereSql(conditionJson));
            ExcelUtils.export(response,"高新区企业（119家）",wrapperList,CrmHighTechZoneEnterprises.class);
            return AjaxResult.success("导出数据成功");
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.error("导出数据失败");
        }
    }

    @Log(title = "高新区企业（119家）", businessType = BusinessType.DELETE)
    @PostMapping("/delete")
    @ApiOperation(value = "删除高新区企业（119家）", notes = "删除高新区企业（119家）")
    public AjaxResult delete(@RequestBody List<String> ids)
    {
        return toAjax(service.removeByIds(ids));
    }

    @DeleteMapping("/deleteAll")
    @ApiOperation(value = "清除数据", notes = "清除数据")
    public AjaxResult deleteAll(){
        return service.deleteAll();
    }

    @PostMapping("/getUploadTemplate")
    @ApiOperation(value = "获取上传模板", notes = "获取上传模板")
    public void getUploadTemplate(HttpServletResponse response) throws IOException {
        DowntemplateUtil.downloadTemplate(response,"怀化高新区企业名单模板");
    }
}

