package cn.com.countrygarden.bi.main.service.domainname.imp;

import cn.com.countrygarden.bi.main.core.Response;
import cn.com.countrygarden.bi.main.core.Result;
import cn.com.countrygarden.bi.main.dao.domainname.DomainCostDataMapper;
import cn.com.countrygarden.bi.main.dao.domainname.DomainnameMapper;
import cn.com.countrygarden.bi.main.dto.domainname.DomainCostDataVO;
import cn.com.countrygarden.bi.main.dto.domainname.DomainNameDTO;
import cn.com.countrygarden.bi.main.model.PageData;
import cn.com.countrygarden.bi.main.model.domainname.DomainCostData;
import cn.com.countrygarden.bi.main.model.domainname.DomainName;
import cn.com.countrygarden.bi.main.service.domainname.DomainnameService;
import com.github.pagehelper.PageRowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
@Transactional
public class DomainnameServiceImpl implements DomainnameService {

    @Autowired
    private DomainnameMapper domainnameMapper;
    @Autowired
    private DomainCostDataMapper domainCostDataMapper;

    @Override
    public Result showDomainnameList(DomainNameDTO domainName, String userCode, int page, int size) {
        PageRowBounds pageRowBounds = new PageRowBounds(page,size);
        Integer isAdmin = domainnameMapper.checkIsDomainAdmin(userCode);
        List<DomainName> totalList = null;
        if(isAdmin > 0) {
            //管理员可查看：本人草稿状态的域名信息， 和有效状态的全部状态的域名信息。
            // （参数：usercode=登记人and audit_status=3  or (所有人的and audit_status！=3 ))
            totalList = domainnameMapper.getDomainnameList(domainName,userCode,pageRowBounds);
        }else {
            //法务部领导可查看：所有已发布、待审核数据
            totalList = domainnameMapper.getDomainnameList(domainName,null,pageRowBounds);
        }
        PageData pageList = new PageData(totalList, pageRowBounds);
        return Response.response(pageList);
    }

    @Override
    public Result add(DomainName domainName, List<DomainCostData> domainCostData, String userCode) {
        DomainName dn = new DomainName();
        dn.setDomain_name(domainName.getDomain_name());
        List<DomainName> list=domainnameMapper.select(dn);
        if (list!=null&&list.size()>0){
            return Response.responseFail("域名重复，请核对再添加");
        }
        //取主表域名赋值给子表域名字段
        int domain_id =domainName.getDomain_id();
        for(int i=0; i < domainCostData.size(); i++){
            domainCostData.get(i).setDomain_id(domain_id);
        }
        //TODO
       // domainName.setDomain_coupon(Calendar.YEAR+"");//域名券号系统自动生成， 规则： 年度+4位流水号
        domainName.setRegister_user(userCode);
        domainName.setRegister_time(new Date());
        //TODO
        domainnameMapper.insert(domainName); //待研究
        if (null != domainCostData && !domainCostData.isEmpty()) {

            domainCostDataMapper.insertList(domainCostData);
        }
        return Response.response("添加成功");
    }

    @Override
    public Result queryById(Integer id) {
        DomainName dom=domainnameMapper.selectByPrimaryKey(id);

        DomainCostData costData=new DomainCostData();
        costData.setDomain_id(id);
        List<DomainCostData> list=domainCostDataMapper.select(costData);

        DomainCostDataVO domainCostDataVO=new DomainCostDataVO();
        domainCostDataVO.setDomainName(dom);
        domainCostDataVO.setDomainCostData(list);
        return Response.response(domainCostDataVO);
    }


    @Override
    public Result edit(DomainName domainName) {

        DomainName dName=new DomainName();
        dName.setDomain_name(domainName.getDomain_name());
        String str =domainName.getDomain_name();
        List<DomainName> list=domainnameMapper.select(dName);
        if (list!=null&&list.size()>0){
            return Response.responseFail("域名重复，请核对再编辑！");
        }

        domainnameMapper.update(domainName);
        return Response.response("修改成功");
    }

    @Override
    public Result publishById(Integer id) {
        domainnameMapper.publish(id);
        return Response.response("发布成功");
    }

    @Override
    public Result deleteMyself(Integer id) {
        //usercode无需校验了，因为上游已过滤只能看到自己草稿
        //前端控制草稿状态的域名信息才可删除
        if (domainnameMapper.deleteMyself(id) > 0) {
            return Response.response("刪除成功");
        }else {
            return Response.response("非法调用");
        }

    }



}

