package cn.com.countrygarden.bi.main.service.experience.impl;

import cn.com.countrygarden.bi.main.core.Result;
import cn.com.countrygarden.bi.main.core.ResultCode;
import cn.com.countrygarden.bi.main.dao.experience.TygInfoManageMapper;
import cn.com.countrygarden.bi.main.dao.twerp.hm.TbSysUserMapper;
import cn.com.countrygarden.bi.main.dto.experience.TygInfoManageDto;
import cn.com.countrygarden.bi.main.dto.manager.TygInfoManage;
import cn.com.countrygarden.bi.main.model.AccessToken;
import cn.com.countrygarden.bi.main.model.twerp.hm.TbSysUser;
import cn.com.countrygarden.bi.main.service.AccessTokenService;
import cn.com.countrygarden.bi.main.service.experience.TygInfoManageService;
import cn.com.countrygarden.bi.main.util.PageUtils;
import com.mysql.jdbc.MysqlDataTruncation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class TygInfoManageServiceImpl implements TygInfoManageService {
    @Autowired
    private TygInfoManageMapper tygInfoManageMapper;
    @Autowired
    private AccessTokenService accessTokenService;
    @Autowired
    private TbSysUserMapper tbSysUserMapper;
    @Value("${wechat.h5.url}")
    private String wechatH5Url;
    @Override
    public Result add(TygInfoManage tygInfoManage, HttpServletRequest req) {
        Result result=new Result();
        Integer id = tygInfoManage.getId();
        String accessToken = req.getParameter("AccessToken");
        AccessToken userAccessToken = accessTokenService.findByToken(accessToken);
        if(userAccessToken ==null){
            result.setResultCode(500);
            result.setMessage("当前登陆账户无法识别，请从重新登录或刷新页面后操作。");
            return result;
        }
        try {
            if (id != null) {
                //编辑
                tygInfoManage.setUpdateBy(userAccessToken.getUserCode());
                tygInfoManage.setUpdateTime(new Date());
                tygInfoManageMapper.updateById(tygInfoManage);

//                TygInfoManage tygInfoManage1 = tygInfoManageMapper.selectById(id.toString());
//                Date effectiveTimeStart = tygInfoManage1.getEffectiveTimeStart();
//                Date date = new Date();
//                boolean before = date.before(effectiveTimeStart);
//                if (before) {
//                    tygInfoManage.setUpdateBy(userAccessToken.getUserCode());
//                    tygInfoManage.setUpdateTime(new Date());
//                    tygInfoManageMapper.updateById(tygInfoManage);
//                } else {
//                    result.setResultCode(10006);
//                    result.setMessage("生效期间内的通知不可修改;");
//                    return result;
//                }
                result.setResultCode(200);
                result.setMessage("编辑信息成功");
                return result;
            }
            String userCode = userAccessToken.getUserCode();
            tygInfoManage.setCreateBy(userCode);
            tygInfoManageMapper.add(tygInfoManage);
            Integer id1 = tygInfoManage.getId();
            tygInfoManage.setUrl(wechatH5Url+id1);
            tygInfoManageMapper.updateById(tygInfoManage);
            result.setResultCode(200);
            result.setMessage("新增成功");
        }catch (Exception e){
            if(e instanceof MysqlDataTruncation){
                result.setResultCode(500);
                result.setMessage("操作失败,内容过大，请调整后上传。");
            }
        }
        return result;
    }

    @Override
    public Result list(TygInfoManageDto tygInfoManageDto) {
        Result result=new Result();
        int n = (tygInfoManageDto.getPage() - 1) * (tygInfoManageDto.getSize());
        tygInfoManageDto.setPageStart(n);
        List<TygInfoManage> list = tygInfoManageMapper.list(tygInfoManageDto);
        for(int i=0;i<list.size();i++){
            TygInfoManage tygInfoManage = list.get(i);
            String createBy = tygInfoManage.getCreateBy();
            TbSysUser byUserCode = tbSysUserMapper.findByUserCode(createBy);
            if(byUserCode !=null){
                String userName = byUserCode.getUserName();
                tygInfoManage.setCreateBy(userName);
            }
        }
        int total = tygInfoManageMapper.count(tygInfoManageDto);
        PageUtils pageUtils = new PageUtils(list,total);
        return result.setResultCode(ResultCode.SUCCESS).setData(pageUtils);
    }

    @Override
    public Result noPageList(TygInfoManageDto tygInfoManageDto) {
        if(tygInfoManageDto ==null){
            tygInfoManageDto=new TygInfoManageDto();
        }
        Result result=new Result();
        tygInfoManageDto.setEffectiveTimeStart(new Date());
        tygInfoManageDto.setEffectiveTimeEnd(new Date());
        List<TygInfoManage> list = tygInfoManageMapper.list(tygInfoManageDto);
        //查询erp账户中文名
        for(int i=0;i<list.size();i++){
            TygInfoManage tygInfoManage = list.get(i);
            String createBy = tygInfoManage.getCreateBy();
            TbSysUser byUserCode = tbSysUserMapper.findByUserCode(createBy);
            if(byUserCode !=null){
                String userName = byUserCode.getUserName();
                tygInfoManage.setCreateBy(userName);
            }
        }
        return result.setResultCode(ResultCode.SUCCESS).setData(list);
    }

    @Override
    public Result one(String id) {
        Result result=new Result();
        TygInfoManage tygInfoManage=tygInfoManageMapper.selectById(id);
        return result.setResultCode(ResultCode.SUCCESS).setData(tygInfoManage);
    }

    @Override
    public Result del(String id) {
        Result result=new Result();
        if(id ==null){
            result.setResultCode(400);
            result.setMessage("id不能为空");
            return result;
        }
        tygInfoManageMapper.del(id);
//        TygInfoManage tygInfoManage1 = tygInfoManageMapper.selectById(id);
//        Date effectiveTimeStart = tygInfoManage1.getEffectiveTimeStart();
//        Date effectiveTimeEnd = tygInfoManage1.getEffectiveTimeEnd();
//        Date date =new Date();
//        boolean before = date.after(effectiveTimeStart);
//        boolean before1 = date.before(effectiveTimeEnd);
//        if(!(before && before1)){
//            tygInfoManageMapper.del(id);
//        }else{
//            result.setResultCode(10006);
//            result.setMessage("生效期间内的通知不可删除;");
//            return result;
//        }
        return result.setResultCode(ResultCode.SUCCESS).setData("");
    }

}
