package com.bonus.hn.bearing.capacity.backstage.project.service;
import com.bonus.hn.bearing.capacity.backstage.companypro.dao.ProjectManagerDao;
import com.bonus.hn.bearing.capacity.backstage.companypro.entity.ProjectManagerBean;
import com.bonus.hn.bearing.capacity.backstage.manager.utils.StringHelper;
import com.bonus.hn.bearing.capacity.backstage.manager.utils.UserUtil;
import com.bonus.hn.bearing.capacity.backstage.project.dao.LineProjectDao;
import com.bonus.hn.bearing.capacity.backstage.project.entity.LineProjectBean;
import org.springframework.jdbc.BadSqlGrammarException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.UnexpectedRollbackException;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.sql.SQLSyntaxErrorException;
import java.util.List;

import static com.bonus.hn.bearing.capacity.backstage.manager.utils.StringHelper.coordinateTransform;

/**
 * APP登录记录表(AppLoginAudit)表服务实现类
 *
 * @author makejava
 * @since 2022-12-12 13:57:46
 */
@Service("LineProjectService")
public class LineProjectImpl implements LineProjectService {
    @Resource
    private LineProjectDao dao;
    @Resource
    private ProjectManagerDao dao1;

    @Override
    public List<LineProjectBean> getProjectTree(LineProjectBean o) {
        return dao.getProjectTree(o);
    }

    @Override
    public List<LineProjectBean> getFirmList(LineProjectBean o) {
        o.setLevel(UserUtil.getLoginUser().getLevel());
        o.setOrgIds(UserUtil.getLoginUser().getOrgId());
        return dao.getFirmList(o);
    }

    @Override
    public int addProjectInfo(LineProjectBean bean) {
        String province = bean.getProvince3();
        String city = bean.getCity3();
        String combinedField = province + "-" + city;
        bean.setCombinedField(combinedField);
        String coordinates = coordinateTransform(Double.parseDouble(bean.getLongitude()), Double.parseDouble(bean.getLineLength()),bean.getMeridian());
        String[] parts = coordinates.split(",");
        // 大地经度
        String geodeticLon = parts[0];
        // 大地纬度
        String geodeticLat = parts[1];
        bean.setGeodeticLon(geodeticLon);
        bean.setGeodeticLat(geodeticLat);
        int proName = dao.getProName(bean);
        if(proName >0){
            return 3;
        }
        return dao.addProjectInfo(bean);
    }

    @Override
    public LineProjectBean getListProject(String id) {
        return dao.getListProject(id);
    }

    @Override
    public int updateProject(LineProjectBean bean) {
        String province = bean.getProvince3();
        String city = bean.getCity3();
        String combinedField = province + "-" + city;
        bean.setCombinedField(combinedField);
        String coordinates = coordinateTransform(Double.parseDouble(bean.getLongitude()), Double.parseDouble(bean.getLineLength()),bean.getMeridian());
        String[] parts = coordinates.split(",");
        // 大地经度
        String geodeticLon = parts[0];
        // 大地纬度
        String geodeticLat = parts[1];
        bean.setGeodeticLon(geodeticLon);
        bean.setGeodeticLat(geodeticLat);
        int proName = dao.getProName(bean);
        if(proName >0){
            return 3;
        }
        return dao.updateProject(bean);
    }

    @Override
    public String delProjectById(String id) {
        //查询是否有绑定关系--如果有 返回前台不予删除
        String code = dao.getIsExistBindRelation(id);
        int number = 0;
        String result = "";
        if (StringHelper.isEmpty(code)){
            number = dao.delProjectById(id);
            if (number == 1){
                result = "1";
            }else {
                result = "0";
            }
        }else {
            result = code;
        }
        return result;
    }

    @Override
    public String upCompletedById(String id) {
        String code = dao.getBindOrNot(id);
        int number = 0;
        String result = "";
        if (StringHelper.isEmpty(code)){
            number = dao.upCompletedById(id);
            if (number == 1) {
                result = "1";
            } else {
                result = "0";
            }
        }else {
            result = code;
        }
        return result;
    }

    @Override
    public List<String> getPermissionList(String id) {
        return dao.getPermissionList(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public String upForcedUnbinding(String id) {
        if (id == null || id.trim().isEmpty()) {
            throw new IllegalArgumentException("id不能为空");
        }
            //查询该项目部下的所有未解绑工程
            List<LineProjectBean> list = dao.getPermissionLists(id);
            //循环list
            for (LineProjectBean bean : list) {
                bean.setUserId(UserUtil.getLoginUser().getId().toString());
                int code ;
                //强制解绑
                int num =  dao.upForcedUnbinding(bean);
                if (num > 0){
                    switch (bean.getLevels()) {
                        case "1":
                            //查询项目管理团队是否有其他绑定关系
                            code = dao.getIsExistRelationship(bean);
                            if (code == 0){
                                //解绑释放工程-项目管理团队绑定关系
                                dao.updateStatus(bean);
                            }
                            break;
                        case "3":
                            //解绑释放工程-自有班组绑定关系
                            dao.updateTeamOrGroup(bean);
                            break;
                        case "5":
                            code = dao.getIsExistOtherRelation(bean);
                            if (code == 0) {
                                //解绑释放工程-分包商-分包班组绑定关系
                                dao.updateSubcontractingTeam(bean);
                            }
                            break;
                        default:
                            break;
                    }
                }
            }
            dao.upCompletedById(id);
            return "1";
    }
}
