package com.diagbot.facade;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.diagbot.client.UserServiceClient;
import com.diagbot.dto.DisclaimerInformationDTO;
import com.diagbot.dto.RespDTO;
import com.diagbot.entity.DisclaimerInformation;
import com.diagbot.enums.IsDeleteEnum;
import com.diagbot.enums.StatusEnum;
import com.diagbot.exception.CommonErrorCode;
import com.diagbot.exception.CommonException;
import com.diagbot.service.impl.DisclaimerInformationServiceImpl;
import com.diagbot.util.DateUtil;
import com.diagbot.util.UserUtils;
import com.diagbot.vo.DisclaimerInformationAllVO;
import com.diagbot.vo.DisclaimerInformationIdVO;
import com.diagbot.vo.DisclaimerInformationVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author wangfeng
 * @Description: TODO
 * @date 2018年11月21日 下午4:25:48
 */
@Component
public class DisclaimerInformationFacaed extends DisclaimerInformationServiceImpl {

    @Autowired
    UserServiceClient userServiceClient;

    
    /**
     * 分页查询免责声明
     * @param page
     * @param title
     * @return
     */
    public List<DisclaimerInformationDTO> getDisclaimerInformation(Page page, String title) {

        DisclaimerInformationAllVO disAll = new DisclaimerInformationAllVO();
        disAll.setTitle(title);
        IPage<DisclaimerInformationDTO> querDisclaimerInformation = getDisclaimerInformationAll(page, disAll);
        List<DisclaimerInformationDTO> data = querDisclaimerInformation.getRecords();

        List<String> modifierid = new ArrayList<>();
        modifierid = data.stream().map(ac -> ac.getModifier()).collect(Collectors.toList());

        Map<String, String> userNames = new HashMap<>();
        if (modifierid.size() > 0) {
            RespDTO<Map<String, String>> userNamesDTO = userServiceClient.getUserInfoByIds(modifierid);
            if (userNamesDTO == null || !"0".equals(userNamesDTO.code)) {
                throw new CommonException(CommonErrorCode.SERVER_IS_ERROR,
                        "获取所有用户信息失败");
            }
            userNames = userNamesDTO.data;
        }


        List<DisclaimerInformationDTO> dataNew = new ArrayList<DisclaimerInformationDTO>();
        for (DisclaimerInformationDTO disclaimerInformationDTO : data) {
            DisclaimerInformationDTO dis = new DisclaimerInformationDTO();
            dis.setId(disclaimerInformationDTO.getId());
            dis.setDescription(disclaimerInformationDTO.getDescription());
            dis.setDisclaimerCode(disclaimerInformationDTO.getDisclaimerCode());
            dis.setGmtCreate(disclaimerInformationDTO.getGmtCreate());
            dis.setModifier(userNames.get(disclaimerInformationDTO.getModifier()));
            dis.setOrderNo(disclaimerInformationDTO.getOrderNo());
            dis.setRemark(disclaimerInformationDTO.getRemark());
            dis.setStatus(disclaimerInformationDTO.getStatus());
            dis.setTitle(disclaimerInformationDTO.getTitle());
            //dis.setVersionId(disclaimerInformationDTO.getVersionId());

            dataNew.add(dis);
        }
        return dataNew;
    }

    /**
     * 增加
     *
     * @return
     */
    public Boolean addDisclaimerInformation(DisclaimerInformationVO disclaimerInformationVO) {
        DisclaimerInformation disclaimerInformation = new DisclaimerInformation();
        disclaimerInformation.setCreator(UserUtils.getCurrentPrincipleID());
        disclaimerInformation.setModifier(UserUtils.getCurrentPrincipleID());
        disclaimerInformation.setDescription(disclaimerInformationVO.getDescription());
        disclaimerInformation.setDisclaimerCode(disclaimerInformationVO.getDisclaimerCode());
        disclaimerInformation.setGmtCreate(DateUtil.now());
        disclaimerInformation.setGmtModified(DateUtil.now());
        disclaimerInformation.setOrderNo(disclaimerInformationVO.getOrderNo());
        disclaimerInformation.setRemark(disclaimerInformationVO.getRemark());
        disclaimerInformation.setStatus(String.valueOf(StatusEnum.Enable.getKey()));
        disclaimerInformation.setTitle(disclaimerInformationVO.getTitle());
        //disclaimerInformation.setVersionId();

        boolean res = save(disclaimerInformation);

        return res;
    }

    /**
     * 修改
     *
     * @return
     */
    public Boolean updateDisclaimerInformation(DisclaimerInformationAllVO disclaimerInformationAllVO) {
        // 1.先判断数据是否存在有效
        checkExist(disclaimerInformationAllVO.getId());
        // 2.修改版本详情信息
        DisclaimerInformation disclaimerInformation = new DisclaimerInformation();

        disclaimerInformation.setGmtModified(DateUtil.now());// 修改时间
        disclaimerInformation.setModifier(UserUtils.getCurrentPrincipleID());
        disclaimerInformation.setDescription(disclaimerInformationAllVO.getDescription());
        disclaimerInformation.setDisclaimerCode(disclaimerInformationAllVO.getDisclaimerCode());
        disclaimerInformation.setGmtCreate(DateUtil.now());
        disclaimerInformation.setId(disclaimerInformationAllVO.getId());
        disclaimerInformation.setOrderNo(disclaimerInformationAllVO.getOrderNo());
        disclaimerInformation.setRemark(disclaimerInformationAllVO.getRemark());
        disclaimerInformation.setStatus(String.valueOf(StatusEnum.Enable.getKey()));
        disclaimerInformation.setTitle(disclaimerInformationAllVO.getTitle());
        disclaimerInformation.setVersionId(0L);

        boolean res = updateById(disclaimerInformation);

        if (!res) {
            throw new CommonException(CommonErrorCode.UPDATE_INFO_FAIL);
        }

        return res;
    }

    /**
     * 删除
     *
     * @return
     */
    public Boolean cancelDisclaimerInformation(DisclaimerInformationIdVO disclaimerInformationIdVO) {
        // 1.先判断数据是否存在有效
        checkExist(disclaimerInformationIdVO.getId());
        UpdateWrapper<DisclaimerInformation> disclaimerInformationNew = new UpdateWrapper<>();
        disclaimerInformationNew.in("id", disclaimerInformationIdVO.getId()).eq("is_deleted", IsDeleteEnum.N.getKey())
                .set("is_deleted", IsDeleteEnum.Y.getKey()).set("modifier", UserUtils.getCurrentPrincipleID())
                .set("gmt_modified", DateUtil.now());
        return update(new DisclaimerInformation(), disclaimerInformationNew);
    }

    /**
     * 启用
     *
     * @return
     */
    public boolean startDisclaimerInformations(DisclaimerInformationIdVO disclaimerInformationIdVO) {
        // 1.先判断数据是否存在有效
        checkExist(disclaimerInformationIdVO.getId());
        UpdateWrapper<DisclaimerInformation> disclaimerInformationNew = new UpdateWrapper<>();
        disclaimerInformationNew.in("id", disclaimerInformationIdVO.getId()).eq("is_deleted", IsDeleteEnum.N.getKey())
                .eq("status", StatusEnum.Disable.getKey()).set("modifier", UserUtils.getCurrentPrincipleID())
                .set("gmt_modified", DateUtil.now()).set("status", StatusEnum.Enable.getKey());
        return update(new DisclaimerInformation(), disclaimerInformationNew);
    }

    /**
     * 停用
     *
     * @return
     */
    public boolean endDisclaimerInformations(DisclaimerInformationIdVO disclaimerInformationIdVO) {
        // 1.先判断数据是否存在有效
        checkExist(disclaimerInformationIdVO.getId());
        UpdateWrapper<DisclaimerInformation> disclaimerInformationNew = new UpdateWrapper<>();
        disclaimerInformationNew.in("id", disclaimerInformationIdVO.getId()).eq("is_deleted", IsDeleteEnum.N.getKey())
                .eq("status", StatusEnum.Enable.getKey()).set("modifier", UserUtils.getCurrentPrincipleID())
                .set("gmt_modified", DateUtil.now()).set("status", StatusEnum.Disable.getKey());
        return update(new DisclaimerInformation(), disclaimerInformationNew);
    }

    /**
     * 判断数据是否存在有效
     *
     * @param id
     */
    private void checkExist(Long id) {
        QueryWrapper<DisclaimerInformation> disclaimerInformationFand = new QueryWrapper<>();
        disclaimerInformationFand.eq("is_deleted", IsDeleteEnum.N.getKey()).eq("id", id);
        int sum = count(disclaimerInformationFand);
        if (sum == 0) {
            throw new CommonException(CommonErrorCode.NOT_EXISTS, "该数据不存在");
        }
    }

}
