/**
 * <p>Title: BiTargetYearController.java</p>
 * <p>
 * <p>Description: </p>
 * <p>
 * <p>Copyright: Copyright (c) 2017</p>
 * <p>
 * <p>Company: www.baidudu.com</p>
 *
 * @author zhaolie
 * @date 2018年6月6日
 * @version 1.0
 */
package cn.com.countrygarden.bi.main.controller;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.*;

import cn.com.countrygarden.bi.main.model.AccessToken;
import cn.com.countrygarden.bi.main.service.AccessTokenService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import cn.com.countrygarden.bi.main.core.Response;
import cn.com.countrygarden.bi.main.model.bii.BiIncomeMonth;
import cn.com.countrygarden.bi.main.model.bii.BiTargetYear;
import cn.com.countrygarden.bi.main.service.bii.BiIncomeMonthService;
import cn.com.countrygarden.bi.main.service.bii.BiTargetYearService;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>Title: BiTargetYearController</p>
 * <p>
 * <p>Description: </p>
 *
 * @author zhaolie
 * @date 2018年6月6日
 */
@RestController("BiTargetYearController")
@RequestMapping("/api/service/datav/bii")
public class BiTargetYearController {
    @Autowired
    private BiTargetYearService biTargetYearService;

    @Autowired
    private BiIncomeMonthService biIncomeMonthService;

    @Autowired
    private AccessTokenService accessTokenService;
    
    
    //房产经营收入年度目标完成率数据查询
    @ResponseBody
    @RequestMapping(value = "/getBiTargetYear", method = {RequestMethod.POST, RequestMethod.GET})
    public Object getBiTargetYear(@RequestParam(value = "year") String year) {
        List<BiTargetYear> bitl = new ArrayList<BiTargetYear>();
        List<BiIncomeMonth> biml = new ArrayList<BiIncomeMonth>();
        Map dataMap = new HashMap();
        BiTargetYear bit = new BiTargetYear();
        BiIncomeMonth bim = new BiIncomeMonth();
        try {
            String corp_cost_id = "";
            bit.setYear(year);
            //居者乐的目标查询
            corp_cost_id = "100000999";
            bit.setCorpCostId(corp_cost_id);
            BiTargetYear homeMusic = this.biTargetYearService.getgetBiTargetYear(bit);
            if (homeMusic != null) {
                dataMap.put("homeMusic", homeMusic.getTargetAmount());
            } else {
                dataMap.put("homeMusic", null);
            }
            //居者乐月度查询
            bim.setYear(year);
            bim.setCorpCostId(corp_cost_id);
            biml = this.biIncomeMonthService.getToMonthIncomeJuZheLe(bim);
            List dataList = new ArrayList();
            if (biml.size() != 0) {
                for (BiIncomeMonth incomeMonth : biml) {
                    if (incomeMonth.getMonth().equals("1")) {
                        dataMap.put("Jan", incomeMonth.getPaidAmount());
                    }
                    if (incomeMonth.getMonth().equals("2")) {
                        dataMap.put("Feb", incomeMonth.getPaidAmount());
                    }
                    if (incomeMonth.getMonth().equals("3")) {
                        dataMap.put("Mar", incomeMonth.getPaidAmount());
                    }
                    if (incomeMonth.getMonth().equals("4")) {
                        dataMap.put("Apr", incomeMonth.getPaidAmount());
                    }
                    if (incomeMonth.getMonth().equals("5")) {
                        dataMap.put("May", incomeMonth.getPaidAmount());
                    }
                    if (incomeMonth.getMonth().equals("6")) {
                        dataMap.put("June", incomeMonth.getPaidAmount());
                    }
                    if (incomeMonth.getMonth().equals("7")) {
                        dataMap.put("July", incomeMonth.getPaidAmount());
                    }
                    if (incomeMonth.getMonth().equals("8")) {
                        dataMap.put("Aug", incomeMonth.getPaidAmount());
                    }
                    if (incomeMonth.getMonth().equals("9")) {
                        dataMap.put("Sept", incomeMonth.getPaidAmount());
                    }
                    if (incomeMonth.getMonth().equals("10")) {
                        dataMap.put("Oct", incomeMonth.getPaidAmount());
                    }
                    if (incomeMonth.getMonth().equals("11")) {
                        dataMap.put("Nov", incomeMonth.getPaidAmount());
                    }
                    if (incomeMonth.getMonth().equals("12")) {
                        dataMap.put("Dec", incomeMonth.getPaidAmount());
                    }

                }
            } else {
                dataMap.put("Jan", null);
                dataMap.put("Feb", null);
                dataMap.put("Mar", null);
                dataMap.put("Apr", null);
                dataMap.put("May", null);
                dataMap.put("June", null);
                dataMap.put("July", null);
                dataMap.put("Aug", null);
                dataMap.put("Sept", null);
                dataMap.put("Oct", null);
                dataMap.put("Nov", null);
                dataMap.put("Dec", null);
            }
            //社区目标查询
            corp_cost_id = "100000516";
            bit.setCorpCostId(corp_cost_id);
            BiTargetYear communityAdd = this.biTargetYearService.getgetBiTargetYear(bit);
            if (communityAdd != null) {
                dataMap.put("communityAdd", communityAdd.getTargetAmount());
            } else {
                dataMap.put("communityAdd", null);
            }
            //房屋租售目标查询
            corp_cost_id = "100000583";
            bit.setCorpCostId(corp_cost_id);
            BiTargetYear secondRental = this.biTargetYearService.getgetBiTargetYear(bit);
            if (secondRental != null) {
                dataMap.put("secondRental", secondRental.getTargetAmount());
            } else {
                dataMap.put("secondRental", null);
            }
            dataList.add(dataMap);
            return Response.response(dataList);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return Response.responseFail("");
        }
    }

    //房产经营收入年度目标完成率数据插入

    /**
     * 使用循环简化代码
     * 插入每个月的汇总数据
     *
     * @ModifiedBy:Geoffrey
     * @Date:2018/06/22 1:05:44
     */
    @ResponseBody
    @RequestMapping(value = "/insertBiTargetYear", method = {RequestMethod.POST, RequestMethod.GET})
    public Object insertBiTargetYear(@RequestParam(value = "year") String year, @RequestParam(value = "homeMusic") String homeMusic, @RequestParam(value = "communityAdd") String communityAdd
            , @RequestParam(value = "secondRental") String secondRental, @RequestParam(value = "Jan") String Jan, @RequestParam(value = "Feb") String Feb
            , @RequestParam(value = "Mar") String Mar, @RequestParam(value = "Apr") String Apr, @RequestParam(value = "May") String May
            , @RequestParam(value = "June") String June, @RequestParam(value = "July") String July, @RequestParam(value = "Aug") String Aug
            , @RequestParam(value = "Sept") String Sept, @RequestParam(value = "Oct") String Oct, @RequestParam(value = "Nov") String Nov
            , @RequestParam(value = "Dec") String Dec, HttpServletRequest request) {
        BiTargetYear bit = new BiTargetYear();
        BiTargetYear bit1 = new BiTargetYear();
        BiIncomeMonth bim = new BiIncomeMonth();
        BiIncomeMonth bim1 = new BiIncomeMonth();

        String accessToken = request.getParameter("AccessToken");
        AccessToken token = accessTokenService.findByToken(accessToken);
		System.out.println("-------------------------------------------");
		System.out.println(accessToken);
		System.out.println(token);
		System.out.println("-------------------------------------------");

        String[] months = {Jan, Feb, Mar, Apr, May, June, July, Aug, Sept, Oct, Nov, Dec};    //构造数组，用来构造循环简化代码

        try {
            String corp_cost_id = "";
            bit.setYear(year);
            //居者乐的目标
            corp_cost_id = "100000999";
            bit.setCorpCostId(corp_cost_id);
            bit1 = this.biTargetYearService.getgetBiTargetYear(bit);
            if (bit1 != null) {
                BigDecimal bd = new BigDecimal(homeMusic);
                bit.setTargetAmount(bd);
                bit.setUpdateDate(new Date());
                if(token==null){
                	bit.setUpdateUserId("0");
                }else{
                	bit.setUpdateUserId(token.getUserCode());
                }
                
                this.biTargetYearService.updateBiTargetYear(bit);
            } else {
                BigDecimal bd = new BigDecimal(homeMusic);
                bit.setTargetAmount(bd);

                bit.setCreatedDate(new Date());
                bit.setUpdateDate(new Date());
                bit.setDeleteFlg("0");
                if(token==null){
                	 bit.setCreatedUserId("0");
                     bit.setUpdateUserId("0");
                }
                else{
                	 bit.setCreatedUserId(token.getUserCode());
                     bit.setUpdateUserId(token.getUserCode());
                }
                bit.setYear(year);
                bit.setCorpCostId("100000999");
                this.biTargetYearService.insertBiTargetYear(bit);
            }
            //社区增值
            corp_cost_id = "100000516";
            BiTargetYear bit2 = new BiTargetYear();
            bit2.setCorpCostId(corp_cost_id);
            bit2.setYear(year);
            bit1 = this.biTargetYearService.getgetBiTargetYear(bit2);
            if (bit1 != null) {
                BigDecimal bd = new BigDecimal(communityAdd);
                bit2.setTargetAmount(bd);
                bit2.setUpdateDate(new Date());
                if(token==null){
                	bit2.setUpdateUserId("0");
                }else{
                	bit2.setUpdateUserId(token.getUserCode());
                }
                this.biTargetYearService.updateBiTargetYear(bit2);
            } else {
                BigDecimal bd = new BigDecimal(communityAdd);
                bit2.setTargetAmount(bd);

                bit2.setCreatedDate(new Date());
                bit2.setUpdateDate(new Date());
                bit2.setDeleteFlg("0");
                if(token==null){
	                bit2.setCreatedUserId("0");
	                bit2.setUpdateUserId("0");
                }else{
                	 bit2.setCreatedUserId(token.getUserCode());
                     bit2.setUpdateUserId(token.getUserCode());
                }
                bit2.setYear(year);
                bit2.setCorpCostId("100000516");
                this.biTargetYearService.insertBiTargetYear(bit2);
            }

            //房屋租售
            corp_cost_id = "100000583";
            BiTargetYear bit3 = new BiTargetYear();
            bit3.setCorpCostId(corp_cost_id);
            bit3.setYear(year);
            bit1 = this.biTargetYearService.getgetBiTargetYear(bit3);
            if (bit1 != null) {
                BigDecimal bd = new BigDecimal(secondRental);
                bit3.setTargetAmount(bd);
                bit3.setUpdateDate(new Date());
                if(token==null){
                	bit3.setUpdateUserId("0");
                }else{
                	bit3.setUpdateUserId(token.getUserCode());
                }
                this.biTargetYearService.updateBiTargetYear(bit3);
            } else {
                BigDecimal bd = new BigDecimal(secondRental);
                bit3.setTargetAmount(bd);
                bit3.setCreatedDate(new Date());
                bit3.setUpdateDate(new Date());
                bit3.setDeleteFlg("0");
                if(token==null){
	                bit3.setCreatedUserId("0");
	                bit3.setUpdateUserId("0");
                }else{
                	bit3.setCreatedUserId(token.getUserCode());
  	                bit3.setUpdateUserId(token.getUserCode());
                }
                bit3.setYear(year);
                bit3.setCorpCostId("100000583");
                this.biTargetYearService.insertBiTargetYear(bit3);
            }

            //汇总目标数据
            //@Modified By:Geoffrey
            //@Date:2018/6/29 14:36
            //[start]
            corp_cost_id = "000000";
            BiTargetYear biTargetYear = new BiTargetYear();
            biTargetYear.setCorpCostId(corp_cost_id);
            biTargetYear.setYear(year);
            bit1 = this.biTargetYearService.getgetBiTargetYear(biTargetYear);
            BigDecimal hm = new BigDecimal(homeMusic);
            BigDecimal ca = new BigDecimal(communityAdd);
            BigDecimal sr = new BigDecimal(secondRental);
            BigDecimal count = hm.add(ca).add(sr);
            if (bit1 != null) {
                biTargetYear.setUpdateDate(new Date());
                if(token==null){
                	 biTargetYear.setUpdateUserId("0");
                }else{
                	 biTargetYear.setUpdateUserId(token.getUserCode());
                }
               
                biTargetYear.setTargetAmount(count);
                this.biTargetYearService.updateBiTargetYear(biTargetYear);
            } else {
                biTargetYear.setTargetAmount(count);
                biTargetYear.setCreatedDate(new Date());
                biTargetYear.setUpdateDate(new Date());
                biTargetYear.setDeleteFlg("0");
                if(token==null){
	                biTargetYear.setCreatedUserId("0");
	                biTargetYear.setUpdateUserId("0");
                }else{
                	biTargetYear.setCreatedUserId(token.getUserCode());
	                biTargetYear.setUpdateUserId(token.getUserCode());
                }
                
                biTargetYear.setYear(year);
                biTargetYear.setCorpCostId(corp_cost_id);
                this.biTargetYearService.insertBiTargetYear(biTargetYear);
            }
            //[end]

            //居者乐月度
            corp_cost_id = "100000999";
            bim.setYear(year);
            bim.setCorpCostId(corp_cost_id);

            for (int i = 1; i < months.length + 1; i++) {
                bim.setMonth(String.valueOf(i));
                bim1 = this.biIncomeMonthService.getToMonthIncomeJuZheLeone(bim);
                if (bim1 != null) {
                    BigDecimal bd = new BigDecimal(months[i - 1]);
                    bim.setPaidAmount(bd);
                    bim.setUpdateDate(new Date());
                    bim.setCreatedDate(new Date());
                    if(token==null){
                    	bim.setUpdateUserId("0");
                    }else{
                    	bim.setUpdateUserId(token.getUserCode());
                    }
                    this.biIncomeMonthService.updateToMonthIncomeJuZheLe(bim);
                } else {
                    BigDecimal bd = new BigDecimal(months[i - 1]);
                    bim.setPaidAmount(bd);
                    bim.setDeleteFlg("0");
                    if(token==null){
	                    bim.setCreatedUserId("0");
	                    bim.setUpdateUserId("0");
                    }else{
                    	bim.setCreatedUserId(token.getUserCode());
                        bim.setUpdateUserId(token.getUserCode());
                    }
                    bim.setYear(year);
                    bim.setMonth(String.valueOf(i));
                    bim.setCorpCostId("100000999");
                    bim.setComCorpCostId("100000998");
                    bim.setCostName("车位");
                    bim.setCreatedDate(new Date());
                    bim.setUpdateDate(new Date());
                    BigDecimal bd1 = new BigDecimal(0);
                    bim.setQoq(bd1);
                    this.biIncomeMonthService.insertToMonthIncomeJuZheLe(bim);
                    this.biIncomeMonthService.insertToMonthIncomeHZJuZheLe(Integer.parseInt(year), i);
                }
            }
            //环比增长率调用存储过程
            this.biIncomeMonthService.getcall();
            return Response.response("true");
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return Response.responseFail("false");
        }
    }
}
