package org.springblade.modules.app.manager;

import com.alibaba.excel.support.ExcelTypeEnum;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wn.common.datamodel.ModelConvertUtil;
import com.wn.common.perfmonitor.annotation.ServiceTimeLog;
import com.wn.common.tools.date.DateUtils;
import com.wn.common.validator.Validator;
import org.springblade.modules.app.common.dto.EnteringZhejiangFilingDTO;
import org.springblade.modules.app.common.dto.UserDTO;
import org.springblade.modules.app.common.enums.StateEnum;
import org.springblade.modules.app.common.enums.UserTypeEnum;
import org.springblade.modules.app.common.query.EnteringZhejiangFilingListQuery;
import org.springblade.modules.app.common.query.EnteringZhejiangFilingQuery;
import org.springblade.modules.app.common.vo.EnteringZhejiangFilingExportVO;
import org.springblade.modules.app.constant.ErrorCode;
import org.springblade.modules.app.service.IEnteringZhejiangFilingService;
import org.springblade.modules.app.utils.ErrorMsg;
import org.springblade.modules.app.utils.ExcelUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author huyinuo
 */
@Slf4j
@Component
@RefreshScope
public class EnteringZhejiangFilingManager {

    /**
     * 省id:山东37，河南41，安徽34，江苏32，上海31，浙江33
     */
    private List<Long> provIds = Arrays.asList(31L, 32L, 33L, 34L, 37L, 41L);

    @Autowired
    private IEnteringZhejiangFilingService enteringZhejiangFilingService;

    public Long addEnteringZhejiangFiling(EnteringZhejiangFilingDTO enteringZhejiangFilingDTO, UserDTO userDTO) {
        Validator.notNull(enteringZhejiangFilingDTO, ErrorCode.INPUT_PARAMETERS_NULL);
        Validator.notNull(enteringZhejiangFilingDTO.getAppendix(), ErrorCode.INPUT_PARAMETERS_NULL);
        Validator.notNull(enteringZhejiangFilingDTO.getEvidence(), ErrorCode.INPUT_PARAMETERS_NULL);
        Validator.notNull(enteringZhejiangFilingDTO.getExpressNumber(), ErrorCode.INPUT_PARAMETERS_NULL);

        Validator.isTrue(userDTO.getType().equals(UserTypeEnum.FARM.getCode()), ErrorMsg.NEW("只有养殖场能创建入浙备案登记！"));
        Validator.isTrue(!provIds.contains(userDTO.getProvId()), ErrorMsg.NEW("山东省、河南省、安徽省、江苏省、上海省 、浙江省以外省份的养殖场才需要创建备案登记！"));

        // 组装数据
        enteringZhejiangFilingDTO.setUserId(userDTO.getId());
        enteringZhejiangFilingDTO.setUserName(userDTO.getUserName());
        enteringZhejiangFilingDTO.setStatus(StateEnum.INIT.getCode());

        Long id = enteringZhejiangFilingService.addEnteringZhejiangFiling(enteringZhejiangFilingDTO);
        return id;
    }

    public EnteringZhejiangFilingDTO getEnteringZhejiangFiling(UserDTO userDTO) {
        Long userId = userDTO.getId();
        return enteringZhejiangFilingService.getEnteringZhejiangFilingByUserId(userId);
    }

    public EnteringZhejiangFilingDTO getEnteringZhejiangFiling(Long id) {
        return enteringZhejiangFilingService.getEnteringZhejiangFilingById(id);
    }

    public Page<EnteringZhejiangFilingDTO> pageEnteringZhejiangFiling(EnteringZhejiangFilingQuery enteringZhejiangFilingQuery) {
        Validator.notNull(enteringZhejiangFilingQuery, ErrorCode.INPUT_PARAMETERS_NULL);
        return enteringZhejiangFilingService.pageEnteringZhejiangFiling(enteringZhejiangFilingQuery);
    }

    public Boolean modifyEnteringZhejiangFiling(Long id) {
        Validator.notNull(id, ErrorCode.INPUT_PARAMETERS_NULL);
        EnteringZhejiangFilingDTO enteringZhejiangFilingDTO = enteringZhejiangFilingService.getEnteringZhejiangFilingById(id);
        Validator.notNull(enteringZhejiangFilingDTO, ErrorMsg.NEW("入浙备案不存在"));
        //Validator.isTrue(enteringZhejiangFilingDTO.getStatus().equals(StateEnum.INIT.getCode()), ErrorMsg.NEW("修改的备案信息状态应该是未查看状态"));
        enteringZhejiangFilingDTO.setStatus(StateEnum.EFFECT.getCode());
        return enteringZhejiangFilingService.updateEnteringZhejiangFiling(enteringZhejiangFilingDTO);
    }

    @ServiceTimeLog
    public void exportEnteringZhejiangFilings(String ids, HttpServletResponse response) {
        String fileName = "export_file_" + DateUtils.getNow(DateUtils.FORMAT_TIMESTAMP);
        String sheetName = "sheet1";

        Validator.notNull(StringUtils.isNotBlank(ids), ErrorCode.INPUT_PARAMETERS_NULL);
        EnteringZhejiangFilingListQuery enteringZhejiangFilingListQuery = new EnteringZhejiangFilingListQuery();
        enteringZhejiangFilingListQuery.setIds(Arrays.stream(ids.split(",")).map(Long::parseLong).collect(Collectors.toList()));
        List<EnteringZhejiangFilingDTO> enteringZhejiangFilingDTOList = enteringZhejiangFilingService.listEnteringZhejiangFilingList(enteringZhejiangFilingListQuery);
        List<EnteringZhejiangFilingExportVO> enteringZhejiangFilingExportVOList = ModelConvertUtil.copyListTo(enteringZhejiangFilingDTOList, EnteringZhejiangFilingExportVO.class);
        ExcelUtil.createExcelStreamByEasyExcel(response, enteringZhejiangFilingExportVOList, fileName, sheetName, ExcelTypeEnum.XLSX);
    }
}
