package com.jeeplus.test.cw.workClientInfo.controller;

import cn.hutool.core.date.DateUnit;
import cn.hutool.core.util.ObjectUtil;
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.jeeplus.aop.logging.annotation.ApiLog;
import com.jeeplus.common.utils.ResponseUtil;
import com.jeeplus.core.excel.ExcelOptions;
import com.jeeplus.core.excel.ExportMode;
import com.jeeplus.core.excel.utils.EasyPoiUtil;
import com.jeeplus.core.query.QueryWrapperGenerator;
import com.jeeplus.sys.constant.enums.LogTypeEnum;
import com.jeeplus.sys.utils.DictUtils;
import com.jeeplus.sys.utils.StringUtils;
import com.jeeplus.test.cw.workClientInfo.service.CwWorkClientService;
import com.jeeplus.test.cw.workClientInfo.service.dto.CwWorkClientBaseDTO;
import com.jeeplus.test.jobPosion.domain.JobPosition;
import com.jeeplus.test.jobPosion.service.dto.JobPositionDTO;
import com.jeeplus.test.jobPosion.service.mapstruct.JobPositionWrapper;
import com.jeeplus.test.klgBase.question.service.dto.KlgBaseQuestionsDTO;
import com.jeeplus.test.knowledge.domain.KnowledgeShareComment;
import com.jeeplus.test.knowledge.domain.KnowledgeShareDetail;
import com.jeeplus.test.knowledge.service.KnowledgeShareInfoService;
import com.jeeplus.test.knowledge.service.dto.KnowledgeShareInfoDto;
import com.jeeplus.test.knowledge.service.dto.KnowledgeShareListDto;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.http.client.utils.DateUtils;
import org.flowable.engine.impl.el.DateUtil;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@Api("客户信息")
@RequestMapping(value = "/cw_work_client/info")
public class CwWorkClientController {

    @Resource
    private CwWorkClientService cwWorkClientService;

    /**
     * 查询客户信息列表
     * @param cwWorkClientBaseDTO
     * @param page
     * @return
     * @throws Exception
     */
    @ApiLog("查询客户信息列表")
    @PreAuthorize("hasAuthority('cw_work_client:info:list')")
    @GetMapping("list")
    public ResponseEntity<IPage<CwWorkClientBaseDTO>> data(CwWorkClientBaseDTO cwWorkClientBaseDTO, Page<CwWorkClientBaseDTO> page) throws Exception {
        IPage<CwWorkClientBaseDTO> result = new Page<CwWorkClientBaseDTO>();
        result = cwWorkClientService.findList (page,cwWorkClientBaseDTO);
        return ResponseEntity.ok (result);
    }

    /**
     * 查询客户信息列表tree
     * @param cwWorkClientBaseDTO
     * @param page
     * @return
     * @throws Exception
     */
    @ApiLog("查询客户信息列表tree")
    @GetMapping("list_tree")
    public ResponseEntity<IPage<CwWorkClientBaseDTO>> dataTree(CwWorkClientBaseDTO cwWorkClientBaseDTO, Page<CwWorkClientBaseDTO> page) throws Exception {
        IPage<CwWorkClientBaseDTO> result = new Page<CwWorkClientBaseDTO>();
        result = cwWorkClientService.findListTree (page,cwWorkClientBaseDTO);
        return ResponseEntity.ok (result);
    }

    /**
     * 查询客户信息数据
     * @param id
     * @return
     */
    @ApiLog("查询客户信息数据")
    @PreAuthorize ("hasAnyAuthority('cw_work_client:info:view','cw_work_client:info:add','cw_work_client:info:edit')")
    @GetMapping("queryById")
    public ResponseEntity queryById(@RequestParam("id") String id) {
        CwWorkClientBaseDTO cwWorkClientBaseDTO = cwWorkClientService.queryById ( id );
        return ResponseEntity.ok (cwWorkClientBaseDTO);
    }

    /**
     * 保存客户信息
     * @param cwWorkClientBaseDTO
     * @return
     */
    @ApiLog(value = "修改/新增客户信息", type = LogTypeEnum.SAVE)
    @PreAuthorize("hasAnyAuthority('cw_work_client:info:add','cw_work_client:info:edit')")
    @PostMapping("save")
    public ResponseEntity save(@Valid @RequestBody CwWorkClientBaseDTO cwWorkClientBaseDTO) throws Exception {
        cwWorkClientService.saveClient(cwWorkClientBaseDTO);
        return ResponseEntity.ok ("操作成功");
    }

    /**
     * 保存客户信息-流程
     * @param cwWorkClientBaseDTO
     * @return
     */
    @ApiLog(value = "保存客户信息-流程", type = LogTypeEnum.SAVE)
    @PreAuthorize("hasAnyAuthority('cw_work_client:info:add','cw_work_client:info:edit')")
    @PostMapping("saveForm")
    public ResponseEntity saveForm(@Valid @RequestBody CwWorkClientBaseDTO cwWorkClientBaseDTO) throws Exception {
        String id = cwWorkClientService.saveForm(cwWorkClientBaseDTO);
        return ResponseUtil.newInstance().add("businessTable", "cw_work_client_base").add("businessId", id).ok ("操作成功");
    }

    /**
     * 删除业务提问
     * @param ids
     * @return
     */
    @ApiLog(value = "删除客户信息", type = LogTypeEnum.SAVE)
    @PreAuthorize ("hasAuthority('cw_work_client:info:del')")
    @DeleteMapping("delete")
    public ResponseEntity delete(String ids) {
        return cwWorkClientService.deleteByIds(ids);
    }

    /**
     * 根据id修改状态值status
     * @param dto
     * @return
     */
    @ApiOperation(value = "根据id修改状态值status")
    @PostMapping(value = "/updateStatusById")
    public ResponseEntity<String> updateStatusById(@RequestBody CwWorkClientBaseDTO dto) {
        String s = cwWorkClientService.updateStatusById(dto);
        return ResponseEntity.ok(s);
    }


    /**
     * 导出客户数据
     *
     * @param cwWorkClientBaseDTO
     * @param page
     * @param response
     * @throws Exception
     */
    @ApiLog(value = "导出客户数据", type = LogTypeEnum.EXPORT)
    @GetMapping("export")
    @PreAuthorize ("hasAuthority('cw_work_client:info:export')")
    @ApiOperation(value = "导出客户数据")
    public void exportFile(CwWorkClientBaseDTO cwWorkClientBaseDTO, Page <CwWorkClientBaseDTO> page, ExcelOptions options, HttpServletResponse response) throws Exception {
        String fileName = options.getFilename();
        String sheetName = options.getSheetName();
        List<CwWorkClientBaseDTO> result = new ArrayList<>();
        if ( ExportMode.current.equals ( options.getMode() ) ) {
            result = cwWorkClientService.findList (page,cwWorkClientBaseDTO).getRecords();
        } else if (ExportMode.selected.equals ( options.getMode() )) {
            result = cwWorkClientService.findList (page,cwWorkClientBaseDTO).getRecords().stream ( ).filter ( item ->
                    options.getSelectIds ( ).contains ( item.getId ( ) )
            ).collect ( Collectors.toList ( ) );
        } else {
            page.setSize (-1);
            page.setCurrent (0);
            result = cwWorkClientService.findList (page,cwWorkClientBaseDTO).getRecords();
        }
        result.stream().forEach(item -> {
            if (StringUtils.isNotBlank(item.getType())){
                item.setType(DictUtils.getDictLabel(item.getType(),"cw_work_client_type",""));
            }
            if (StringUtils.isNotBlank(item.getIndustry())){
                item.setIndustry(DictUtils.getDictLabel(item.getIndustry(),"cw_work_client_industry",""));
            }
            if (StringUtils.isNotBlank(item.getIsListed())) {
                item.setIsListed(DictUtils.getDictLabel(item.getIsListed(),"yes_no",""));
            }
            if (StringUtils.isNotBlank(item.getAffiliation())){
                item.setAffiliation(DictUtils.getDictLabel(item.getAffiliation(),"cw_work_client_affiliation",""));
            }
            if (StringUtils.isNotBlank(item.getOwnershipType())){
                item.setOwnershipType(DictUtils.getDictLabel(item.getOwnershipType(),"cw_work_client_ownership_type",""));
            }
            if (StringUtils.isNotBlank(item.getReportType())){
                item.setReportType(DictUtils.getDictLabel(item.getReportType(),"cw_work_client_report_type",""));
            }
            if (StringUtils.isNotBlank(item.getStatus())){
                item.setStatus(DictUtils.getDictLabel(item.getStatus(),"cw_status",""));
            }
        });
        EasyPoiUtil.exportExcel ( result, sheetName,  sheetName, CwWorkClientBaseDTO.class, fileName, response );

    }


}
