package com.sw.mes.yb.seed.service.impl;

import com.sw.mes.core.base.BaseServiceImpl;
import com.sw.mes.core.context.BaseContextHandler;
import com.sw.mes.core.response.Result;
import com.sw.mes.core.response.ResultGenerator;
import com.sw.mes.yb.seed.dao.SeedRequireTaskDao;
import com.sw.mes.yb.seed.dto.SeedRequireTaskDto;
import com.sw.mes.yb.seed.entity.SeedRequireTask;
import com.sw.mes.yb.seed.service.SeedRequireTaskService;
import com.sw.mes.yb.seed.utils.CodeSender;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
public class SeedRequireTaskServiceImpl extends BaseServiceImpl<SeedRequireTask> implements SeedRequireTaskService {
    private SeedRequireTaskDao dao;

    @Autowired
    public void setDao(SeedRequireTaskDao dao) {
        this.dao = dao;
        super.setBaseDao(dao);
    }

    @Override
    public SeedRequireTask findByCode(String code) {
        return dao.findByCode(code);
    }

    @Override
    public Result create(SeedRequireTaskDto seedRequireTaskDto) {
        SeedRequireTask seedRequireTask = dao.findByCode(seedRequireTaskDto.getCode());
        if (!ObjectUtils.isEmpty(seedRequireTask)) {
            return ResultGenerator.fail("规则名称已存在,请重新输入!");
        }
        String code = seedRequireTaskDto.getCode();
        code = StringUtils.isEmpty(code) ? CodeSender.getNumber("RW", 5) : code;
        seedRequireTaskDto.setCode(code);
        if (null == seedRequireTaskDto.getAllocatedQuantity()) {
            seedRequireTaskDto.setAllocatedQuantity(new BigDecimal(0));
        }
        if (null == seedRequireTaskDto.getAllotQuantity()) {
            seedRequireTaskDto.setAllotQuantity(new BigDecimal(0));
        }
        seedRequireTaskDto.setCreateUser(BaseContextHandler.getUsername());
        seedRequireTaskDto.setCreateTime(new Date());
        seedRequireTaskDto.setDel(false);
        this.saveOrUpdate(fromDto(seedRequireTaskDto));
        return ResultGenerator.success();
    }

    @Override
    public Result update(SeedRequireTaskDto seedRequireTaskDto) {
        SeedRequireTask seedRequireTask = dao.findByCode(seedRequireTaskDto.getCode());
        if (ObjectUtils.isEmpty(seedRequireTask)) {
            return ResultGenerator.fail("规则不存在,请检查数据!");
        }
        BigDecimal noAllocatedQuantity = seedRequireTask.getNoAllocatedQuantity();
        BigDecimal allocatedQuantity = seedRequireTask.getAllocatedQuantity();
        BigDecimal allotQuantity = seedRequireTaskDto.getAllotQuantity();
        if (allotQuantity.compareTo(noAllocatedQuantity) == 1) {
            return ResultGenerator.fail("未分配需求数量小于分配数量,请检查数据!");
        }
        BigDecimal allocatedQuantity1 = allocatedQuantity.add(allotQuantity);
        seedRequireTask.setAllocatedQuantity(allocatedQuantity1);
        seedRequireTask.setDel(seedRequireTask.getRequireCount(), seedRequireTask.getAllocatedQuantity());
        seedRequireTask.setNoAllocatedQuantity(seedRequireTask.getRequireCount(), seedRequireTask.getAllocatedQuantity());
        seedRequireTask.setUpdateUser(BaseContextHandler.getUsername());
        seedRequireTask.setUpdateTime(new Date());
        this.saveOrUpdate(seedRequireTask);
        return ResultGenerator.success();
    }

    @Override
    public List<SeedRequireTask> findOldSeedRequireTask() {
        SimpleDateFormat sdf =  new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = new Date();
        String endTime = sdf.format(date);
        return dao.findOldSeedRequireTask(endTime);
    }

    @Override
    public List<SeedRequireTask> findSeedRequireTask() {
        SimpleDateFormat sdf =  new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = new Date();
        String endTime = sdf.format(date);
        return dao.findSeedRequireTask(endTime);
    }

    public SeedRequireTask fromDto(SeedRequireTaskDto dto) {
        SeedRequireTask seedRequireTask = new SeedRequireTask();
        BeanUtils.copyProperties(dto, seedRequireTask);
        seedRequireTask.setDel(dto.getRequireCount(), dto.getAllocatedQuantity());
        seedRequireTask.setNoAllocatedQuantity(dto.getRequireCount(), dto.getAllocatedQuantity());
        return seedRequireTask;
    }
}
