package cn.ey.biz.module.system.service.code;

import cn.ey.biz.common.pojo.PageParam;
import cn.ey.biz.common.pojo.PageResult;
import cn.ey.biz.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.ey.biz.module.system.api.code.dto.CodeCustomerDTO;
import cn.ey.biz.module.system.controller.admin.code.vo.CodeCustomerVO;
import cn.ey.biz.module.system.controller.admin.code.vo.HistoryVO;
import cn.ey.biz.module.system.convert.code.CodeCustomerConvert;
import cn.ey.biz.module.system.dal.dataobject.code.CodeCustomerDO;
import cn.ey.biz.module.system.dal.dataobject.user.AdminUserDO;
import cn.ey.biz.module.system.dal.mapper.code.CodeCustomerMapper;
import cn.ey.biz.module.system.service.user.AdminUserService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.stream.Collectors;

@Service
@Validated
@Slf4j
public class CodeCustomerServiceImpl implements CodeCustomerService {

    @Resource
    private CodeCustomerMapper codeCustomerMapper;

    @Resource
    private AdminUserService adminUserService;

    @Override
    @Transactional
    public Long submitCustomer(CodeCustomerVO codeCustomerVO) {

        LambdaUpdateWrapper<CodeCustomerDO> varWrapper = new LambdaUpdateWrapper<>();
        varWrapper.eq(CodeCustomerDO::getActive, 1);
        varWrapper.set(CodeCustomerDO::getActive, 0);
        codeCustomerMapper.update(null, varWrapper);

        CodeCustomerDO codeCustomerDO = CodeCustomerConvert.INSTANCE.vo2Do(codeCustomerVO);
        codeCustomerMapper.insert(codeCustomerDO);
        return codeCustomerDO.getId();
    }

    @Override
    public PageResult<HistoryVO> getHistory(PageParam pageParam) {
        PageResult<CodeCustomerDO> pageDo = codeCustomerMapper.selectPage(pageParam,
                new LambdaQueryWrapperX<CodeCustomerDO>().orderByDesc(CodeCustomerDO::getCreateTime));
        PageResult<HistoryVO> pageHistory = new PageResult<>();
        pageHistory.setList(pageDo.getList().stream().map(doo -> {
            HistoryVO vo = CodeCustomerConvert.INSTANCE.do2History(doo);
            String updater = doo.getUpdater();
            AdminUserDO user = adminUserService.getUser(Long.valueOf(updater));
            if (user != null) {
                vo.setUpdateUser(user.getUsername());
            }
            return vo;
        }).collect(Collectors.toList()));
        pageHistory.setTotal(pageDo.getTotal());
        return pageHistory;
    }

    @Override
    public CodeCustomerDTO getCodeCustomer(Long ver) {
        LambdaQueryWrapper<CodeCustomerDO> query = new LambdaQueryWrapper<>();
        if (ver != null) {
            query.eq(CodeCustomerDO::getId, ver);
        } else {
            query.eq(CodeCustomerDO::getActive, 1);
        }
        CodeCustomerDO doo = codeCustomerMapper.selectOne(query);
        CodeCustomerDTO dto = CodeCustomerConvert.INSTANCE.do2Dto(doo);

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss");
        dto.setDate(sdf.format(doo.getCreateTime()));
        AdminUserDO user = adminUserService.getUser(Long.valueOf(doo.getUpdater()));
        if (user != null) {
            dto.setUpdater(user.getUsername());
        }
        return dto;
    }

    @Override
    public void revertHistory(Long ver) {

    }
}
