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.CrmOutputValue;
import com.zksy.system.basicData.service.CrmOutputValueService;
import com.zksy.utils.DowntemplateUtil;
import com.zksy.utils.SearchUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
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;

/**
 * 企业产值Controller
 *
 * @author sy
 * @date 2024-06-04
 */
@RestController
@RequestMapping("/crmOutputValue")
@Api(tags = "企业产值", description = "企业产值desc")
public class CrmOutputValueController extends BaseController{

    @Autowired
    private CrmOutputValueService service;

    @GetMapping("/getById/{borrowId}")
    @ApiOperation(value = "企业产值搜索getById")
    public CrmOutputValue getById(@PathVariable String borrowId) {
        return service.getById(borrowId);
    }

    @GetMapping("/findByPage")
    @ApiOperation(value = "企业产值分页")
    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 = "企业产值查询所有")
    public List<CrmOutputValue> getList(String conditionJson) throws Exception {
        return service.list(SearchUtil.parseWhereSql(conditionJson));
    }

    /**
     * 新增企业产值
     */
    @PostMapping("/save")
    @ApiOperation(value = "企业产值新增")
    @Log(title = "新增企业产值", businessType = BusinessType.INSERT)
    public boolean save(@RequestBody CrmOutputValue crmOutputValue) {
        return service.save(crmOutputValue);
    }

    /**
     * 修改企业产值
     */
    @PostMapping("/updateById")
    @ApiOperation(value = "企业产值修改")
    @Log(title = "修改企业产值", businessType = BusinessType.UPDATE)
    public boolean updateById(@RequestBody CrmOutputValue crmOutputValue) {
        return service.updateById(crmOutputValue);
    }

    /**
     * 导入企业产值
     */
    @ApiOperation("企业产值导入")
    @Log(title = "企业产值", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file)
    {
        try {
            List<CrmOutputValue> crmOutputValueAll = service.list();
            List<CrmOutputValue> list = ExcelUtils.readMultipartFile(file, CrmOutputValue.class,CrmOutputValue.class.getDeclaredFields().length - 2);
            if(crmOutputValueAll != null && crmOutputValueAll.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("企业产值导出")
    @Log(title = "企业产值", businessType = BusinessType.EXPORT)
    @PostMapping("/exportData")
    public AjaxResult exportData(HttpServletResponse response, String conditionJson){
        try {
            List<CrmOutputValue> wrapperList = service.list(SearchUtil.parseWhereSql(conditionJson));
            ExcelUtils.export(response,"企业产值",wrapperList,CrmOutputValue.class);
            return AjaxResult.success("导出数据成功");
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.error("导出数据失败");
        }
    }

    @Log(title = "企业产值", businessType = BusinessType.DELETE)
    @PostMapping("/delete")
    @ApiOperation(value = "删除企业产值", notes = "删除企业产值")
    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,"企业产值模板");
    }

    @GetMapping("/getListYearOrQuarter")
    @ApiOperation(value = "获取列表的年份产值或者季度产值", notes = "获取列表的年份产值或者季度产值")
    public AjaxResult getListYearOrQuarter(){
        return service.getListYearOrQuarter();
    }

    @GetMapping("/getListByQuarterYoY")
    @ApiOperation(value = "获取上一个季度的列表同比", notes = "获取上一个季度的列表同比")
    public AjaxResult getListByQuarterYoY(@RequestParam("quarter") Integer quarter){
        return service.getListByQuarterYoY(quarter);
    }
}

