package com.bzd.system.service.gxhpz;

import com.bzd.common.config.dao.DaoBase;
import com.bzd.common.config.dao.DaoSupport;
import com.bzd.common.config.dao.PageData;
import com.bzd.common.utils.DateUtils;
import com.bzd.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

import static com.bzd.common.utils.ShiroUtils.getSysUser;

@Service
public class PharmacistsService {
    @Autowired
    private DaoBase dao;
    @Resource(name = "daoSupport")
    private DaoSupport daoSupport;

    /**
     * 查询列表
     * @param pd
     * @return
     * @throws Exception
     */
    public List<PageData> findForList(final PageData pd) throws Exception {
        return (List<PageData>) daoSupport.findForList("pharmacistsMapper.selectPharmacists", pd);
    }

    /**
     * 添加
     * @param pd
     * @return
     * @throws Exception
     */
    @Transactional(rollbackFor = Exception.class)
    public int save(final PageData pd) throws Exception {
        pd.put("createTime", DateUtils.getTime());
        pd.put("createBy", getSysUser().getUserName());
        String default_follow_up = (String) pd.get("default_follow_up");
        String storeIdPage = (String) pd.get("storeId");
        String position = (String) pd.get("position");
        if (StringUtils.isEmpty(storeIdPage)) {
            pd.put("storeId", getSysUser().getDeptId());
        }
        if (StringUtils.isNotEmpty(default_follow_up)) {
            if(default_follow_up.equals("1") && position.equals("任务跟进人")){
                pd.put("default_follow_up", 2);//否
                daoSupport.save("pharmacistsMapper.updateDefaultFollowUpByStoreId", pd);
                pd.put("default_follow_up", 1);//是
            }
        }

        return  daoSupport.save("pharmacistsMapper.insertPharmacists", pd);
    }

    /**
     * 删除
     * @param ids
     * @return
     * @throws Exception
     */
    public Integer del(List<String> ids) throws Exception {
        return daoSupport.delete("pharmacistsMapper.deletePharmacists", ids);
    }

    /**
     * 修改配置
     * @param pd
     * @return
     * @throws Exception
     */
    public Integer update(PageData pd) throws Exception {
        pd.put("updateTime", DateUtils.getTime());
        pd.put("updateBy", getSysUser().getUserName());
        String default_follow_up = (String) pd.get("default_follow_up");
        String storeIdPage = (String) pd.get("storeId");
        String position = (String) pd.get("position");
        if (StringUtils.isEmpty(storeIdPage)) {
            pd.put("storeId", getSysUser().getDeptId());
        }
        if (StringUtils.isNotEmpty(default_follow_up)) {
            if(default_follow_up.equals("1") && position.equals("任务跟进人")){
                pd.put("default_follow_up", 2);//否
                daoSupport.save("pharmacistsMapper.updateDefaultFollowUpByStoreId", pd);
                pd.put("default_follow_up", 1);//是
            }
        }
        return daoSupport.update("pharmacistsMapper.updatePharmacists", pd);
    }
}
