package org.jeecg.modules.userCode.service.impl;


import org.jeecg.common.exception.JeecgBootException;
import org.jeecg.common.util.DateUtils;
import org.jeecg.common.util.RedisUtil;
import org.jeecg.config.shiro.ShiroRealm;
import org.jeecg.modules.banner.service.IWebBannerService;
import org.jeecg.modules.hanHaiMember.entity.HanHaiMember;
import org.jeecg.modules.hanHaiMember.service.IHanHaiMemberService;
import org.jeecg.modules.tbTask.entity.TbTask;
import org.jeecg.modules.tbTask.service.ITbTaskService;
import org.jeecg.modules.userCode.model.req.TaskReq;
import org.jeecg.modules.userCode.model.req.TaskReq2;
import org.jeecg.modules.userCode.service.UserService;
import org.jeecg.modules.userRole.entity.TbUserRole;
import org.jeecg.modules.userRole.service.ITbUserRoleService;
import org.jeecg.modules.utils.ValidateTool;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Date;
import java.util.Map;


@Service
public class UserServiceImpl implements UserService {


    @Resource
    private ITbTaskService tbTaskService;

    @Autowired
    private IHanHaiMemberService iHanHaiMemberService;
    @Autowired
    private RedisUtil redisUtil;
    @Resource
    private ITbUserRoleService tbUserRoleService;
    @Resource
    private ShiroRealm shiroRealm;

    @Transactional
    @Override
    public void addTask(TaskReq tbTask, String token) {
        HanHaiMember hanHaiMember = shiroRealm.checkUserTokenIsEffectHanHaiAccount(token);

        if (ValidateTool.isNull(tbTask.getIndustryId())) {
            throw new JeecgBootException("请填写工种");
        }
        Map<String, TbUserRole> roleInfo = tbUserRoleService.getRoleInfo(hanHaiMember.getId(),true);
        TbUserRole company = roleInfo.get("company");
        TbUserRole boss = roleInfo.get("boss");
        if (ValidateTool.isNull(company) && ValidateTool.isNull(boss)){
            throw new JeecgBootException("资料未审核通过，不能发布任务");
        }
        TbTask task = new TbTask();
        task.setUserId(hanHaiMember.getId());
        task.setRole(0);
        task.setAuditStatus(0);
        if (ValidateTool.isNotNull(company)){
            task.setCompanyName( company.getCompanyName());
            task.setUserName(company.getUserName());
        }else {
            task.setCompanyName("");
            task.setUserName(boss.getUserName());
        }
        task.setHeadPic(hanHaiMember.getHeadImage());
        BeanUtils.copyProperties(tbTask, task);
        float untilHours = DateUtils.getUntilHours(tbTask.getEndTime(), tbTask.getStartTime());
        BigDecimal bigDecimal = new BigDecimal(String.valueOf(untilHours));
        BigDecimal bigDecimal1 = bigDecimal.setScale(2, RoundingMode.HALF_UP);
        task.setWorkTime(bigDecimal1+"h");
        tbTaskService.save(task);

    }


    @Transactional
    @Override
    public void addTask2(TaskReq2 tbTask, String token) {
        HanHaiMember hanHaiMember = shiroRealm.checkUserTokenIsEffectHanHaiAccount(token);

        if (ValidateTool.isNull(tbTask.getIndustryId())) {
            throw new JeecgBootException("请填写工种");
        }
        Map<String, TbUserRole> roleInfo = tbUserRoleService.getRoleInfo(hanHaiMember.getId(),true);
        TbUserRole worker = roleInfo.get("worker");
        if (ValidateTool.isNull(worker)){
            throw new JeecgBootException("资料未审核通过，不能发布任务");
        }
        TbTask task = new TbTask();
        task.setUserId(hanHaiMember.getId());
        task.setRole(1);
        task.setAuditStatus(0);
        task.setHeadPic(hanHaiMember.getHeadImage());
        task.setWorkName(worker.getUserName());
        task.setPhone(worker.getPhone());
        task.setGender(worker.getGender());
        BeanUtils.copyProperties(tbTask, task);
        tbTaskService.save(task);
    }
}
