package com.storlead.sales.controller.customer;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.storlead.framework.common.result.Result;
import com.storlead.sales.pojo.dto.customer.CustomerFollowUpEmailsSearchDTO;
import com.storlead.sales.pojo.dto.customer.CustomerFollowUpEmailsUpdateDTO;
import com.storlead.sales.pojo.vo.customer.CustomerFollowUpEmailsListVO;
import com.storlead.sales.pojo.entity.mail.CustomerMailBingMarkEntity;
import com.storlead.sales.service.customer.CustomerFollowUpEmailsService;
import com.storlead.mail.service.EmailsService;
import com.storlead.sales.service.CustomerMailBingMarkService;
import com.storlead.sales.service.CustomerMailRecordService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import javax.annotation.Resource;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * VIEW 前端控制器
 * </p>
 *
 * @author YPZ
 * @since 2024-10-28
 */
@RestController
@RequestMapping("/customer-follow-up-emails")
@Api(tags = "销售: 客户管理相关接口")
public class CustomerFollowUpEmailsController {
    @Resource
    private CustomerFollowUpEmailsService customerFollowUpEmailsService;
    @Resource
    private EmailsService emailsService;
    @Resource
    private CustomerMailRecordService customerMailRecord;
    @Resource
    private CustomerMailBingMarkService customerMailBingMarkService;

    @PostMapping(value = "/list")
    @ApiOperation(value = "跟进邮件联查-列表", notes = "跟进邮件联查-列表")
    public Result<IPage<CustomerFollowUpEmailsListVO>> list(@RequestBody CustomerFollowUpEmailsSearchDTO customerFollowUpEmailsSearchDTO) {
        Result result = customerFollowUpEmailsService.selectListVO(customerFollowUpEmailsSearchDTO);
        return result;
    }

    @PostMapping(value = "/test")
    @ApiOperation(value = "跟进邮件联查-列表111", notes = "跟进邮件联查-列表111")
    public Result list() {
       Long a = 0L;
        return Result.result(a);
    }

    @PostMapping(value = "/edit-follow-status")
    @ApiOperation(value = "跟进邮件联查-批量修改跟进状态", notes = "跟进邮件联查-批量修改跟进状态")
    @Transactional(rollbackFor = Throwable.class)
    public Result<?> editFollowStatus(@RequestBody CustomerFollowUpEmailsUpdateDTO emailsUpdateDTO) {
        if (CollectionUtils.isEmpty(emailsUpdateDTO.getIdList())){
            return Result.error("参数错误");
        }
        //获取主邮件信息
        LambdaQueryWrapper<CustomerMailBingMarkEntity> customerMailBingMarkEntityLambdaQueryWrapper = new LambdaQueryWrapper<>();
        customerMailBingMarkEntityLambdaQueryWrapper.in(CustomerMailBingMarkEntity::getId, emailsUpdateDTO.getIdList());
        List<CustomerMailBingMarkEntity> customerMailBingMarkEntityList = customerMailBingMarkService.list(customerMailBingMarkEntityLambdaQueryWrapper);

        if (CollectionUtils.isEmpty(customerMailBingMarkEntityList)){
            return Result.error("查询数据异常");
        }
        for (CustomerMailBingMarkEntity customerMailRecord : customerMailBingMarkEntityList){
            if (!ObjectUtils.isEmpty(customerMailRecord.getFollowUpId()) && customerMailRecord.getFollowUpId() != 0){
                return Result.error("有邮件已关联跟进，无法修改！");
            }
        }
        List<Long> customerMailBingMarkIdList = customerMailBingMarkEntityList.stream().map(CustomerMailBingMarkEntity::getId).collect(Collectors.toList());
        if (emailsUpdateDTO.getFollowStatus() == 0){
            LambdaUpdateWrapper<CustomerMailBingMarkEntity> wrapper0 = new LambdaUpdateWrapper<>();
            wrapper0.in(CustomerMailBingMarkEntity::getId,customerMailBingMarkIdList);
            wrapper0.set(CustomerMailBingMarkEntity::getFollowUpId,null);
            wrapper0.set(CustomerMailBingMarkEntity::getFollowUpTime,null);
            customerMailBingMarkService.update(wrapper0);
        }else {
            LambdaUpdateWrapper<CustomerMailBingMarkEntity> wrapper1 = new LambdaUpdateWrapper<>();
            wrapper1.in(CustomerMailBingMarkEntity::getId,customerMailBingMarkIdList);
            wrapper1.isNull(CustomerMailBingMarkEntity::getFollowUpId);
            wrapper1.set(CustomerMailBingMarkEntity::getFollowUpId,0);
            wrapper1.set(CustomerMailBingMarkEntity::getFollowUpTime, LocalDateTime.now());
            customerMailBingMarkService.update(wrapper1);
        }

        return Result.ok();
    }
}
