package com.jeeplus.finance.projectRecords.service;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.extra.spring.SpringUtil;
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.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import com.jeeplus.common.TokenProvider;
import com.jeeplus.core.query.QueryWrapperGenerator;
import com.jeeplus.finance.projectRecords.domain.CwManHourManagement;
import com.jeeplus.finance.projectRecords.mapper.CwManHourManagementMapper;
import com.jeeplus.finance.projectRecords.service.dto.CwManHourManagementDTO;
import com.jeeplus.finance.projectRecords.service.mapstruct.CwManHourManagementWrapper;
import com.jeeplus.sys.domain.User;
//import com.jeeplus.sys.mapper.UserMapper;
import com.jeeplus.sys.feign.IUserApi;
import com.jeeplus.sys.service.dto.UserDTO;
//import com.jeeplus.sys.utils.UserUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 财务项目登记service
 * @author: 徐滕
 * @version: 2022-11-10 14:56
 */
@Service
@Transactional
public class CwManHourManagementService extends ServiceImpl<CwManHourManagementMapper, CwManHourManagement> {

    @Resource
    private CwManHourManagementMapper cwManHourManagementMapper;
//    @Resource
//    private UserMapper userMapper;

    /**
     * 差选项目列表信息
     * @param page
     * @param cwManHourManagementDTO
     * @return
     * @throws Exception
     */
    public IPage<CwManHourManagementDTO> findList(Page<CwManHourManagementDTO> page, CwManHourManagementDTO cwManHourManagementDTO) throws Exception{
        QueryWrapper<CwManHourManagement> queryWrapper = QueryWrapperGenerator.buildQueryCondition ( CwManHourManagementWrapper.INSTANCE.toEntity(cwManHourManagementDTO), CwManHourManagement.class );
        queryWrapper.eq("a.del_flag","0");
        queryWrapper.orderByDesc("a.create_time");
        if (ObjectUtil.isNotEmpty(cwManHourManagementDTO)) {
            if (ArrayUtil.isNotEmpty(cwManHourManagementDTO.getCreateDates())) {
                queryWrapper.between("a.create_time", cwManHourManagementDTO.getCreateDates()[0], cwManHourManagementDTO.getCreateDates()[1]);
            }
            if (StringUtils.isNotBlank(cwManHourManagementDTO.getProjectMasterName())) {
                List<User> userList = SpringUtil.getBean ( IUserApi.class ).selectListByName(cwManHourManagementDTO.getProjectMasterName());
                List<String> ids = userList.stream().distinct().map(User::getId).collect(Collectors.toList());
                if (CollectionUtil.isNotEmpty(ids)) {
                    queryWrapper.in("a.project_master_id", ids);
                } else {
                    return new Page<>();
                }
            }
            if (StringUtils.isNotBlank(cwManHourManagementDTO.getCreateId())){
                queryWrapper.eq("a.create_by_id", cwManHourManagementDTO.getCreateId());
            }
        }
        if(StringUtils.isNotBlank(cwManHourManagementDTO.getProjectName())){
            queryWrapper.like("p.project_name", cwManHourManagementDTO.getProjectName());
        }
        IPage<CwManHourManagementDTO> list = cwManHourManagementMapper.findList(page, queryWrapper);
        List<CwManHourManagementDTO> records = list.getRecords();
        for (CwManHourManagementDTO info:records) {
            if("0".equals(info.getStatus())){
                info.setStatusStr("未确认");
            }else if("1".equals(info.getStatus())){
                info.setStatusStr("已确认");
            }
        }
        return list;
    }

    /**
     * 根据id查询项目详情
     * @param id
     * @return
     */
    public CwManHourManagementDTO queryById(String id) {
        CwManHourManagementDTO cwManHourManagementDTO = cwManHourManagementMapper.queryById(id);
        if (ObjectUtil.isNotEmpty(cwManHourManagementDTO)) {
            if (ObjectUtil.isNotEmpty(cwManHourManagementDTO.getPlanStartDate()) && ObjectUtil.isNotEmpty(cwManHourManagementDTO.getPlanEndDate())) {
                Date[] planDate = new Date[2];
                planDate[0] = cwManHourManagementDTO.getPlanStartDate();
                planDate[1] = cwManHourManagementDTO.getPlanEndDate();
                cwManHourManagementDTO.setPlanDate(planDate);
            }
        }
        return cwManHourManagementDTO;
    }

    /**
     * 保存项目以及其他相关信息
     * @param cwManHourManagementDTO
     * @return
     * @throws Exception
     */
    public String saveForm(CwManHourManagementDTO cwManHourManagementDTO) throws Exception{
        if(StringUtils.isBlank(cwManHourManagementDTO.getId())){
            //获取当前登录人信息
            UserDTO userDTO = SpringUtil.getBean ( IUserApi.class ).getByToken(TokenProvider.getCurrentToken());
//            UserDTO userDTO = UserUtils.getCurrentUserDTO();
        }
        CwManHourManagement cwManHourManagement = CwManHourManagementWrapper.INSTANCE.toEntity(cwManHourManagementDTO);
        if (ObjectUtil.isNotEmpty(cwManHourManagement)) {
            if (StringUtils.isBlank(cwManHourManagement.getCreateById())) {
                cwManHourManagement.setCreateById(SpringUtil.getBean ( IUserApi.class ).getByToken(TokenProvider.getCurrentToken()).getId());
            }
        }
        if(StringUtils.isNotBlank(cwManHourManagementDTO.getProjectId())){
            cwManHourManagement.setProjectId(cwManHourManagementDTO.getProjectId());
        }
        if(cwManHourManagementDTO.getPlanDate().length>1){
            cwManHourManagement.setPlanStartDate(cwManHourManagementDTO.getPlanDate()[0]);
            cwManHourManagement.setPlanEndDate(cwManHourManagementDTO.getPlanDate()[1]);
        }
        this.saveOrUpdate(cwManHourManagement);
        return cwManHourManagement.getId();
    }

    /**
     * 保存项目信息
     * @param cwManHourManagementDTO
     * @return
     * @throws Exception
     */
    public ResponseEntity save(CwManHourManagementDTO cwManHourManagementDTO) throws Exception{
        this.saveForm(cwManHourManagementDTO);
        return ResponseEntity.ok("保存成功");
    }

    /**
     * 保存项目信息
     * @param id
     * @return
     * @throws Exception
     */
    public ResponseEntity updateStatus(String id){
        cwManHourManagementMapper.updateStatus(id);
        return ResponseEntity.ok("保存成功");
    }

    /**
     * 根据id删除项目信息
     * @param ids
     * @return
     */
    public ResponseEntity deleteByIds(String ids) {
        String idArray[] =ids.split(",");
        this.removeByIds (Lists.newArrayList (idArray));
        return ResponseEntity.ok ("删除成功");
    }

    /**
     * 根据客户id获取关联的项目信息
     * @param clientId
     * @return
     */
    /*public List<CwManHourManagementDTO> getByClientId(String clientId,CwManHourManagementDTO cwManHourManagementDTO) throws Exception {
        if(StringUtils.isNotBlank(clientId)){
            List<String> projectIdByClientId = cwManHourManagementMapper.getProjectIdByClientId(clientId);
            List<String> projectList = projectIdByClientId.stream().distinct().collect(Collectors.toList());
            if (CollectionUtil.isNotEmpty(projectList)) {
                QueryWrapper<CwManHourManagement> queryWrapper = QueryWrapperGenerator.buildQueryCondition ( CwManHourManagementWrapper.INSTANCE.toEntity(cwManHourManagementDTO), CwManHourManagement.class );
                queryWrapper.in("a.id", projectList);
                queryWrapper.eq("a.del_flag","0");
                queryWrapper.eq("a.status","5");
                queryWrapper.orderByDesc("a.create_date");
                return cwManHourManagementMapper.getProjectByIdList(queryWrapper);
            } else {
                return new ArrayList<>();
            }
        } else {
            return new ArrayList<>();
        }
    }*/

    /**
     * 根据合同id获取关联的项目信息
     * @param contractId
     * @return
     */
    /*public List<CwManHourManagementDTO> getByContractId(String contractId,CwManHourManagementDTO cwManHourManagementDTO) throws Exception{
        if (StringUtils.isNotBlank(contractId)) {
            QueryWrapper<CwManHourManagement> queryWrapper = QueryWrapperGenerator.buildQueryCondition ( CwManHourManagementWrapper.INSTANCE.toEntity(cwManHourManagementDTO), CwManHourManagement.class );
            queryWrapper.eq("a.contract_id", contractId);
            queryWrapper.eq("a.del_flag","0");
            queryWrapper.eq("a.status","5");
            queryWrapper.orderByDesc("a.create_date");
            return cwManHourManagementMapper.getByContractId(queryWrapper);
        }
        return new ArrayList<>();
    }*/

}
