package cc.iotkit.manager.service.impl;

import cc.iotkit.common.api.PageRequest;
import cc.iotkit.common.api.Paging;
import cc.iotkit.data.manager.IWorderData;
import cc.iotkit.manager.config.BeanUtils;
import cc.iotkit.manager.config.TimestampIdentifierGenerator;
import cc.iotkit.manager.dto.vo.worder.WorderPageReqVO;
import cc.iotkit.manager.dto.vo.worder.WorderRespVO;
import cc.iotkit.manager.dto.vo.worder.WorderSaveReqVO;
import cc.iotkit.manager.service.IWorderService;
import cc.iotkit.model.worder.WorderDO;
import cn.hutool.core.util.ObjectUtil;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.service.spi.ServiceException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import java.util.Date;

import static cc.iotkit.common.satoken.utils.LoginHelper.getLoginUser;


/**
 * 意见反馈评价 Service 实现类
 *
 * @author zhp
 */
@Service
@Slf4j
public class WorderServiceImpl implements IWorderService {

    @Autowired
    private IWorderData iWorderData;
    @Override
    @Transactional(rollbackFor = {Exception.class, RuntimeException.class, ServiceException.class})
    public Long createWorder(WorderSaveReqVO createReqVO) {
        // 插入
     //   String id = TimestampIdentifierGenerator.getIdCreateWorder();
      //  createReqVO.setId(id);
        // 生成工单编号
        /*StringBuilder sb = new StringBuilder("FW");
        String timeStap = DateUtil.format(new Date(), "yyyyMMddHHmmssSSS");
        sb.append(timeStap);
        int sequence = counter.get().getAndIncrement();
        // 如果需要，可以在这里添加逻辑来处理同一秒内超过999个工单的情况     保证三位数，不足前面补0
        String sequencePart = String.format("%03d", sequence);
        sb.append(sequencePart);*/
        createReqVO.setWorderNum(TimestampIdentifierGenerator.getIdCreateWorderNum());
       // TdProductDO tdProductDO = tdProductMapper.selectById(createReqVO.getProductId());
        WorderDO worder = BeanUtils.toBean(createReqVO, WorderDO.class);
        if(worder != null && ObjectUtils.isEmpty(worder.getCustomerId())){
            worder.setCustomerId(getLoginUser().getUserId());
        }
   /*     if (ObjectUtil.isNotEmpty(tdProductDO)) {
            worder.setCustomerAreaId(tdProductDO.getAreaId());
            worder.setCustomerAddress(tdProductDO.getAreaAddress());
            AppScanReturnDataProduct bean = JSONUtil.toBean(tdProductDO.getAppBindingDevice(), AppScanReturnDataProduct.class);
            String cityProvinceRegion = bean.getCityProvinceRegion();
            String detailedAddress = bean.getDetailedAddress();
            worder.setCustomerAddress(cityProvinceRegion + detailedAddress);
        }*/


        worder.setReportTime(new Date());
        iWorderData.save(worder);
        //assignSpByTid(worder.getId());
        // 返回
        return null;
    }

    @Override
    @Transactional(rollbackFor = {Exception.class, RuntimeException.class, ServiceException.class})
    public void updateWorder(WorderSaveReqVO updateReqVO) {
        // 校验存在
        validateWorderExists(updateReqVO.getId());
        // 更新
        WorderDO worderDO1 = iWorderData.findById(updateReqVO.getId());
        if(ObjectUtil.isNotEmpty(updateReqVO.getWorderStatus())) {
            if (updateReqVO.getWorderStatus().equals(worderDO1.getWorderStatus())) {
                throw new RuntimeException("状态不能一样");
            }
        }
        WorderDO updateObj = BeanUtils.toBean(updateReqVO, WorderDO.class);
        iWorderData.save(updateObj);
    }

    @Override
    @Transactional(rollbackFor = {Exception.class, RuntimeException.class, ServiceException.class})
    public void deleteWorder(Long id) {
        // 校验存在
        validateWorderExists(id);
        // 删除
        iWorderData.deleteById(id);
    }

    private void validateWorderExists(Long id) {
        if (iWorderData.findById(id) == null) {
            throw new RuntimeException("不存在");
        }
    }

    @Override
    public WorderDO getWorder(Long id) {
        WorderDO worderDO = iWorderData.findById(id);
      //  whetherEncapsulationCommented(worderDO);
        return worderDO;
    }
    @Override
    public Paging<WorderRespVO> getWorderPage(PageRequest<WorderPageReqVO> pageReqVO) {
       // Paging<WorderRespVO>
//PageRequest<FeedBackEvaluationPageReqVO> pageReqVO
        Paging<WorderRespVO> ddd= iWorderData.findAll(pageReqVO.to(WorderDO.class)).to(WorderRespVO.class);
        return iWorderData.findAll(pageReqVO.to(WorderDO.class)).to(WorderRespVO.class);
    }

}