package com.storlead.sales.controller.customer;


import com.storlead.framework.common.result.Result;
import com.storlead.sales.pojo.dto.customer.CustomerFollowUpSearchDTO;
import com.storlead.sales.pojo.dto.customer.SendRecordAddDTO;
import com.storlead.sales.pojo.dto.customer.SendRecordSearchDTO;
import com.storlead.sales.pojo.entity.customer.SendRecordEntity;
import com.storlead.sales.pojo.vo.customer.SendRecordDetailVO;
import com.storlead.sales.service.customer.MessageAssembleService;
import com.storlead.sales.service.customer.SendRecordService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.springframework.beans.BeanUtils;
import javax.annotation.Resource;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Objects;

/**
 * <p>
 * 寄样记录表 前端控制器
 * </p>
 *
 * @author YPZ
 * @since 2024-04-25
 */
@RestController
@RequestMapping("/send-record")
@Api(tags = "销售: 客户管理相关接口")
public class SendRecordController {

    @Resource
    private SendRecordService sendRecordService;
    @Resource
    private MessageAssembleService assembleService;

    @PostMapping(value = "/add")
    @ApiOperation(value = "客户寄样记录管理-新增/修改客户寄样记录", notes = "客户寄样记录管理-新增/修改客户寄样记录")
    @Transactional(rollbackFor = Throwable.class)
    public Result<?> add(@RequestBody SendRecordAddDTO sendRecordAddDTO) {
        if (ObjectUtils.isEmpty(sendRecordAddDTO)){
            return Result.error("参数不能为空");
        }
        boolean isAdd = false;
        if (ObjectUtils.isEmpty(sendRecordAddDTO.getId())){
            isAdd = true;
        }
        SendRecordEntity sendRecord = new SendRecordEntity();
        BeanUtils.copyProperties(sendRecordAddDTO, sendRecord);
        if (Objects.nonNull(sendRecord.getRecordAmount())) {
            BigDecimal recordAmount = sendRecord.getRecordAmount().setScale(2, RoundingMode.HALF_UP);
            sendRecord.setRecordAmount(recordAmount);
        }
        boolean isOk = sendRecordService.saveOrUpdate(sendRecord);
        if (isAdd){
            assembleService.sendCustomerEventMessages(sendRecordAddDTO.getCustomerId(), "ADD_SAMPLE");
        }

        if (isOk){
            return Result.result("操作成功");
        }
        return Result.error("处理失败!");
    }

    @PostMapping(value = "/list")
    @ApiOperation(value = "客户寄样记录查询-列表", notes = "客户寄样记录查询-列表")
    @ApiResponses({
            @ApiResponse(code = 200, message = "", response = CustomerFollowUpSearchDTO.class)
    })
    public Result<?> list(@RequestBody SendRecordSearchDTO sendRecordSearchDTO) {

        return sendRecordService.selectListVO(sendRecordSearchDTO);

    }

    @GetMapping(value = "/get-by-id")
    @ApiOperation(value = "客户寄样记录查询-通过id查看详情", notes = "客户寄样记录查询-通过id查看详情")
    public Result<SendRecordDetailVO> getById(@RequestParam(name="id",required=false) Long id) {

        SendRecordDetailVO  sendRecordDetailVO = sendRecordService.getSendRecordVOById(id);
        return Result.result(sendRecordDetailVO);
    }

    @ApiOperation(value = "客户寄样记录管理-删除", notes = "客户寄样记录管理-删除")
    @PostMapping("delete")
    public Result delete(Long id) {
        if(Objects.isNull(id)) {
            return Result.error("参数错误");
        }
        sendRecordService.lgDelete(id);
        return Result.ok();
    }

}
